使用以下代码,我能够比较两个文件并找到它们不同的起始位置,但现在我想找到它们不同的所有位置并打印出来,而不仅仅是第一个位置。我知道这是一个循环,但我不知道如何进行循环。我认为保留 while 循环并删除 break 语句会起作用,但它不起作用。 我怎么调用它:
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 %d\n", 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 %d\n", count);
}
count++;
}
关于c - 查找两个文件不同的所有字节位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21373059/