Hi all em có bt đang bí về đọc FILE trong c++ mong ae giúp đỡ.
Đề: cho file txt nội dung chứa nội dung:
4
0 1 1 0
1 0 1 0
1 1 0 0
0 0 0 0
bool docMaTran(char* file, int &soDinh, bool** &maTran)
{
FILE* f = fopen(file, "r");
if (f == NULL)
return false;
fscanf(f, "%d", &soDinh);//Doc so dinh
//doc ma tran
maTran= new bool*[soDinh];// cap phat vung nho
for(int i=0; i<soDinh; i++)
maTran[i] = new bool [soDinh];
{
for(int i=0; i<soDinh; i++)
{
for(int j= 0; j<soDinh; j++)
fscanf(f, "%d", &maTran);
}
}
fclose(f);
return true;
}
MAIN
nt _tmain(int argc, _TCHAR* argv[])
{
// 1. Đọc dữ liệu
char* file = "input.txt";
int soDinh= 0; // Khởi tạo
bool** maTran= NULL; // Khởi tạo
bool kq= docMaTran(file, soDinh, maTran);
if (kq== false)
{
printf("Cannot open file %s\n", file);
return 1;
}
// Test
printf("so dinh la: %d\n", soDinh);
printf("Ma Tran: \n");
for (int i = 0; i < soDinh; i++)
{
for (int j = 0; j < soDinh; j++)
{
printf("%d ", maTran[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
Em đọc được số đỉnh còn ma trận ko đọc được, mong chỉ giáo