为什么 (!(feof(sp)) && end) 永远不会评估为真?
Why does the (!(feof(sp)) && end) never evaluate to true?
我试图创建一个程序来比较两个文件并检查第二个文件是否存在于第一个文件中。
函数 r_scan 进入死循环。
我尝试调试程序并按照 (!(feof(sp)) && end) 的输出进行操作,但我仍然不明白为什么会这样。
这是我的代码:
int r_scan(char* virus_address, char* scaned_address)
{
//INIT
char v = ' ', s = ' ';
int end = 1, i = 0;
long seek = 0;
FILE* vp = NULL;
FILE* sp = NULL;
//Open th5e files.
vp = fopen(virus_address, "rb");
sp = fopen(scaned_address, "rb");
fseek(vp, 0, SEEK_SET);
fseek(sp, 0, SEEK_SET);
//pre scanning
seek = 0;
//Check if the file infected.
while (!(feof(sp)) && end)
{
fread(&s, 1, 1, sp);
if (v == s)
{
v = fgetc(vp);
}
else
{
seek++;
fseek(vp, 0, SEEK_SET);
fseek(sp, seek, SEEK_SET);
fread(&v, 1, 1, vp);
}
if (v == EOF)
{
end = 0;
}
}
fclose(vp);
fclose(sp);
return end;
}
fseek
的手册页说:
A successful call to the fseek() function clears the end-of-file indicator
当你的程序循环时,fseek(sp, seek, SEEK_SET);
在 feof(sp)
之前被调用,这就是为什么 feof
总是 returns 0。而且因为你没有测试 return fread(&s, 1, 1, sp);
的值你的程序将永远循环。
我试图创建一个程序来比较两个文件并检查第二个文件是否存在于第一个文件中。 函数 r_scan 进入死循环。 我尝试调试程序并按照 (!(feof(sp)) && end) 的输出进行操作,但我仍然不明白为什么会这样。
这是我的代码:
int r_scan(char* virus_address, char* scaned_address)
{
//INIT
char v = ' ', s = ' ';
int end = 1, i = 0;
long seek = 0;
FILE* vp = NULL;
FILE* sp = NULL;
//Open th5e files.
vp = fopen(virus_address, "rb");
sp = fopen(scaned_address, "rb");
fseek(vp, 0, SEEK_SET);
fseek(sp, 0, SEEK_SET);
//pre scanning
seek = 0;
//Check if the file infected.
while (!(feof(sp)) && end)
{
fread(&s, 1, 1, sp);
if (v == s)
{
v = fgetc(vp);
}
else
{
seek++;
fseek(vp, 0, SEEK_SET);
fseek(sp, seek, SEEK_SET);
fread(&v, 1, 1, vp);
}
if (v == EOF)
{
end = 0;
}
}
fclose(vp);
fclose(sp);
return end;
}
fseek
的手册页说:
A successful call to the fseek() function clears the end-of-file indicator
当你的程序循环时,fseek(sp, seek, SEEK_SET);
在 feof(sp)
之前被调用,这就是为什么 feof
总是 returns 0。而且因为你没有测试 return fread(&s, 1, 1, sp);
的值你的程序将永远循环。