使用以下代码,我可以比较两个文件,并找到它们不同的开始位置,但现在想找到它们不同的地方并打印出来,而不仅仅是第一位。我知道这是一个循环,但我不知道如何做循环。我以为离开时循环,并竭尽全力将其起作用,但行之有效。我怎么称呼:
if ((fp1 != NULL) && (fp2 != NULL)){
compare_two_binary_files(fp1, fp2);
}
如何实现:
void compare(FILE *f1, FILE *f2)
{
char ch1, ch2;
int flag = 0;
while (((ch1 = fgetc(f1)) != EOF) &&((ch2 = fgetc(f2)) != EOF)){
if (ch1 == ch2)
{
flag = 1;
continue;
}
/*
* If not equal then returns the byte position
*/
else
{
fseek(f1, -1, SEEK_CUR);
flag = 0;
printf("Byte pos where two files differ is %dn", ftell(f1)+1);
break;
}
}
}
尝试
int count = 0;
while (((ch1 = fgetc(f1)) != EOF) &&((ch2 = fgetc(f2)) != EOF)){
if (ch1 != ch2)
{
printf("Byte pos where two files differ is %dn", count);
}
count++;
}