【发布时间】:2015-05-29 17:04:21
【问题描述】:
这是 C 代码的一部分。我需要帮助来修复它。
该程序检查文件签名是否在另一个文件中。
如果是,则该函数找到匹配项然后返回1,否则返回0。
问题是它总是返回0,即使它应该返回1。
这是我写的函数:
int scanFile(char* file_name, FILE * virus_signature, long virus_size) //Scan the given file to see if he has the signature
{
FILE * file_for_scan = fopen(file_name, "rb");
char ch_temp, ch_temp2;
int i = 0;
fseek(virus_signature, 0, SEEK_SET);
while ((ch_temp = fgetc(file_for_scan)) != EOF)
{
if ((ch_temp2=fgetc(virus_signature)) == ch_temp)
{
i++;
if (i == virus_size)
{
fclose(file_for_scan);
return 1;
}
}
else
{
i = 0;
fseek(virus_signature, 0, SEEK_SET);
}
}
fclose(file_for_scan);
return 0;
}
请帮我修复我的代码。
【问题讨论】:
-
解释你的代码有什么问题
-
你到底有什么问题?
-
程序运行不正常。它总是返回 0 而不是应该返回的 1。
-
while (((result2=fread(virus_buffer,1,sizeof(char),virus))>0)&&flag==1),我觉得你需要把flag==1从这里去掉,这里需要改一下逻辑 -
你在很大程度上过于复杂了。为什么不直接将整个文件读入内存然后使用
memmem()(或者循环加上memcmp(),如果它不可用)来查找针的位置?