我不知道为什么这段代码不起作用。
while (TRUE)
{
do
{
fread(buffer1, BUFFER_LEN - 1, 1, pFile);
fread(buffer2, BUFFER_LEN - 1, 1, pVirus);
if (feof(pVirus))
{
printf("MATCH!\n");
flag = 1;
break;
}
} while (strcmp(buffer1, buffer2) == 0);
if (buffer1 != buffer2)
{
rewind(pVirus);
}
if (feof(pFile) || flag == 1)
{
break;
}
}
fclose(pFile);
fclose(pVirus);
代码查找匹配较大文件中的小文件,代码无效。
答案 0 :(得分:0)
以下提议的代码:
现在建议的代码:
rewind( pFile );
do
{
size_t buf1Count = fread(buffer1, 1, BUFFER_LEN, pFile);
if( !buf1Count )
{
if( feof( pFile ) )
{ // reached end of pFile
???
break;
}
else
{ // error event
perror( "fread of pFile failed" );
fclose( pFile );
fclose( pVirus );
exit( EXIT_FAILURE );
}
}
rewind( pVirus );
do
{
size_t buf2Count = fread(buffer2, 1, BUFFER_LEN, pVirus);
if( !buf2Count )
{
if( feof( pVirus ) )
{ // reached end of pVirus
printf( "NO MATCH\n" );
break;
}
else
{ // error event
perror( "fread of pVirus failed" );
fclose( pFile );
fclose( pVirus );
exit( EXIT_FAILURE );
}
}
if( memcmp(buffer1, buffer2, buf1Count) == 0)
{
printf( "MATCH\n" );
break;
}
} while(1);
} while (1);
fclose(pFile);
fclose(pVirus);