Mình có code bài này nhưng không hiểu lắm về nó bạn nào có thể giải thích giùm mình với:
Code:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#define MAX 20
int n = 0;
int canh = 0;
int mt[MAX][MAX];
void readfile();
void writefile();
void readfile()
{
FILE *f = fopen("D:\\exercise\\TTNT\\bai tap co ban\\file\\readfile1.txt","rt");
if(f == NULL)
{
printf("readfile error");
return;
}
int i,j;
i = 0;
j = 0;
if(!feof(f))
{
fscanf(f,"%d",&n);
}
memset(mt,sizeof(mt),0);
while(!feof(f))
{
fscanf(f,"%d%d",&i,&j);
mt[i][j] = 1;
mt[j][i] = 1;
}
fclose(f);
}
void writefile()
{
int i;
FILE *f = fopen("D:\\exercise\\TTNT\\bai tap co ban\\file\\writefile1.txt","wt");
if(f == NULL)
{
printf("readfile error");
return;
}
for(int i = 0; i < n; i++)
{
fprintf(f," \t%d",i);
}
fprintf(f,"\n");
for(i = 0; i < n; i++)
{
fprintf(f,"%d\t",i);
for(int j = 0; j < n; j++)
{
fprintf(f,"%d\t",mt[i][j]);
}
fprintf(f,"\n");
}
fclose(f);
}
void main()
{
readfile();
writefile();
}
Ở đây mình không hiểu sau khi gán tất cả các phần tử của mảng = 0 rồi thì lệnh
Code:
while(!feof(f))
{
fscanf(f,"%d%d",&i,&j);
mt[i][j] = 1;
mt[j][i] = 1;
}
dùng để làm gì và cái gì được gọi để đưa vào i và j ?
Còn đây là file read:
PHP Code:
5 5
0 1
2 3
1 4
2 1
3 4
Tại sao sau khi dùng hàm write thì tạo ra file thành thế này ?
PHP Code:
0 1 2 3 4
0 0 1 0 0 0
1 1 0 1 0 1
2 0 1 0 1 0
3 0 0 1 0 1
4 0 1 0 1 0