Compare two files Byte by Byte

前端 未结 1 1099
不知归路
不知归路 2020-12-21 05:21

I have two binary files and I want to compare them Byte by Byte. I came up with the following code to do so:

int CompareFiles(char *pFname1, char *pFname2)
{         


        
1条回答
  •  借酒劲吻你
    2020-12-21 06:19

    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

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

    int err = OK;
    for (i=0;i

    0 讨论(0)
提交回复
热议问题