问题描述
我有两个二进制文件,我想通过字节来比较它们字节。我想出了以下code这样做的:
INT CompareFiles(字符* pFname1,字符* pFname2)
{
FILE * pFile1,* pFile2;
长lSize1,lSize2; //文件长度
INT I = 0;
焦炭TMP1,TMP2; pFile1 = FOPEN(pFname1,R);
pFile2 = FOPEN(pFname2,R); //获得文件大小:
fseek的(pFile1,0,SEEK_END);
lSize1 = FTELL(pFile1);
倒带(pFile1); //获得文件大小:
fseek的(pFile2,0,SEEK_END);
lSize2 = FTELL(pFile2);
倒带(pFile2); 如果(lSize1!= lSize2){
的printf(文件大小不同,%d个主场迎战%d个\\ N,lSize1,lSize2);
返回(ERROR);
}
对于(i = 0; I< lSize1;我++){
FREAD(安培; TMP1,sizeof的(炭),1,pFile1 + I);
FREAD(安培; TMP2,sizeof的(炭),1,pFile2 + I);
如果(TMP1!= TMP2){
的printf(%!X:TMP1了0x%X = TMP2为0x%X \\ n,我,TMP1,TMP2);
}
}
回报(OK);
}
但由于某些原因,它看起来像在文件中的指针不前进,它保持了整个长度比较相同的字节彼此为
循环。为什么这样?我在做什么错在这里?
FREAD(安培; TMP1,sizeof的(炭),1,pFile1 + I);
FREAD(安培; TMP2,sizeof的(炭),1,pFile2 + I);
时,改变文件句柄循环的每次迭代。您应该使用
FREAD(安培; TMP1,1,1,pFile1);
的fread(安培; TMP2,1,1,pFile2);
来代替。到 FREAD
每次通话将推动文件句柄的内部指针,它会自动的文件内容。
请注意,您也登录文件内容的差异,但未能将错误返回到你的为
循环中调用code。
如果你想,只要你遇到不同的回报,用
为(i = 0; I< lSize1;我++){
的fread(安培; TMP1,1,1,pFile1);
的fread(安培; TMP2,1,1,pFile2);
如果(TMP1!= TMP2){
的printf(%!X:TMP1了0x%X = TMP2为0x%X \\ n,我,TMP1,TMP2);
返回(ERROR); //报告错误给调用者
}
}
回报(OK);
如果你想记录所有的差异(这将是可能非常耗时),用
INT ERR = OK;
对于(i = 0; I< lSize1;我++){
的fread(安培; TMP1,1,1,pFile1);
的fread(安培; TMP2,1,1,pFile2);
如果(TMP1!= TMP2){
的printf(%!X:TMP1了0x%X = TMP2为0x%X \\ n,我,TMP1,TMP2);
ERR =错误; //报告错误给调用者
}
}
返回ERR;
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)
{
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 );
}
But for some reason, it looks like the pointer in the file doesn't advance and it keeps comparing the same Bytes to each other for the whole length of the for
loop. Why so? What am I doing wrong here?
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;
这篇关于比较两个文件逐字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!