2016-06-11 86 views
1

我试图创建一个程序来比较两个文件,并检查第一个文件中是否存在第二个文件。 函数r_scan进入无限循环。 有人知道为什么它不工作?无尽的while循环 - C编程

这是我的代码:

/** 
    The function scan the file and check if there is a virus in the file. 

    Input: 
    virus_address - The address of the virus file. 
    scaned_address - The adress of the file that we scan. 
    Output: 
    end - If the the file infected. 0 - infected, 1 - not infected. 
    **/ 

    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; 
} 
+0

“病毒扫描程序”:D –

+0

“freado(&s,1,1,sp)”的“预扫”是什么意思? – VolAnd

+0

没有我只是忘了改变它。 – liad

回答

3

fseek手册页说:

成功调用FSEEK()函数清除档案结尾指标

当程序循环时,在feof(sp)之前调用fseek(sp, seek, SEEK_SET);,这就是为什么feof始终返回0。而且因为你没有测试返回值fread(&s, 1, 1, sp);你的程序永远循环。