what happens is it gets all the input from fp1 first and then gets input from fp2. Why is it that way? Can’t you get input from 2 different file pointers at the same time inside a while statement?
/*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;
}
Your while statement is short circuiting. When using the OR (
||) operator, if the first expression is true, the second doesn’t not get executed.I’m not entirely sure what you’re trying to achieve with the OR (
||) operator, maybe you actually need an AND (&&)?