发生的情况是它首先从 fp1 获取所有输入,然后从 fp2 获取输入。为什么会这样?您不能在 while 语句中同时从 2 个不同的文件指针获取输入吗?
/*checks if 2 text files are identical */
#include <stdio.h>
int main(void)
{
FILE *fp1,*fp2;
char buf1,buf2;
int flag = 1;
fp1 = fopen("textfile1.txt","r");
fp2 = fopen("textfile2.txt","r");
/* putting them inside a while statement causes a logical error? why */
while(fscanf(fp1,"%c",&buf1) == 1 ||fscanf(fp2,"%c",&buf2) == 1)
{
printf("buf1: %c, buf2: %c\n",buf1,buf2);
if(buf1 != buf2)
{
flag = 0;
//break;
}
}
if(flag == 1)
printf("SAME");
else
printf("NOT SAME");
fclose(fp1);
fclose(fp2);
return 0;
}