我有两个二进制文件,我想比较它们Byte by Byte。我想出了下面的代码这样做:比较两个文件Byte Byte
int CompareFiles(char *pFname1, char *pFname2)
{
FILE *pFile1,*pFile2;
long lSize1, lSize2; // file length
int i=0;
char tmp1, tmp2;
pFile1 = fopen(pFname1,"r");
pFile2 = fopen(pFname2,"r");
// obtain file size:
fseek (pFile1 , 0 , SEEK_END);
lSize1 = ftell (pFile1);
rewind (pFile1);
// obtain file size:
fseek (pFile2 , 0 , SEEK_END);
lSize2 = ftell (pFile2);
rewind (pFile2);
if (lSize1 != lSize2) {
printf("File sizes differ, %d vs. %d\n",lSize1,lSize2);
return (ERROR);
}
for (i=0;i<lSize1;i++) {
fread(&tmp1, sizeof(char), 1, pFile1+i);
fread(&tmp2, sizeof(char), 1, pFile2+i);
if (tmp1 != tmp2) {
printf("%x: tmp1 0x%x != tmp2 0x%x\n",i , tmp1, tmp2);
}
}
return (OK);
}
但由于某些原因,它看起来像在文件中的指针不前进,它保持了整个长度比较相同字节彼此for
循环。为什么这样?我在这里做错了什么?
提示:使用memcmp而不是迭代每个字节:http://www.cplusplus.com/reference/cstring/memcmp/?kw=memcmp –