Compare two files Byte by Byte

非 Y 不嫁゛ 提交于 2019-12-01 20:56:31
fread(&tmp1, sizeof(char), 1, pFile1+i);
fread(&tmp2, sizeof(char), 1, pFile2+i);

is changing the file handle for each iteration of the loop. You should use

fread(&tmp1, 1, 1, pFile1);
fread(&tmp2, 1, 1, pFile2);

instead. Each call to fread will advance the file handle's internal pointer to it's file content automatically.

Note that you also log differences in file content but fail to return an error to calling code during your for loop.

If you want to return as soon as you encounter a difference, use

for (i=0;i<lSize1;i++) {
    fread(&tmp1, 1, 1, pFile1);
    fread(&tmp2, 1, 1, pFile2);
    if (tmp1 != tmp2) {
        printf("%x: tmp1 0x%x != tmp2 0x%x\n",i , tmp1, tmp2);
        return ( ERROR ); // report error to caller
    }
}
return ( OK );

If you want to log all differences (this will be potentially very time consuming), use

int err = OK;
for (i=0;i<lSize1;i++) {
    fread(&tmp1, 1, 1, pFile1);
    fread(&tmp2, 1, 1, pFile2);
    if (tmp1 != tmp2) {
        printf("%x: tmp1 0x%x != tmp2 0x%x\n",i , tmp1, tmp2);
        err = ERROR;  // report error to caller
    }
}
return err;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!