bool isIdentical(FILE *file1, FILE *file2) //this fun tell me files is identical or no
{
// this must have char1 in file1?
char c1 = fgetc(file1);
// this must have char2 in file2?
char c2 = fgetc(file2);
while (c1 != EOF && c2 != EOF) //check if files reached to end
{
c1 = fgetc(file1);
c2 = fgetc(file2);
if (c1 != c2)
return 0;
// I expect to print each char in to files but it actual print NULL
printf("%c %c\n", c1, c2);
}
// this to check if two files reached to end without returning 0
if (c1 == EOF && c2 == EOF)
return 1;
else
return 0; // then the files don't have the same length hence are not identical
}
您必须使用类型
c1
而不是 c2
来定义 int
和 char
,才能可靠地测试 EOF
。 fgetc()
有257个不同的返回值,char
只能容纳256个。