-2

我的代码中的一个条件不起作用。这个条件是如果第一个文件变成 4 行,第二个文件变成 5 行。两个文件的前 4 行相同,但第二个文件的第 5 行可能不同或相同。我的输出必须显示“是的,第 5 行有不同”,但它表示这些文件是相同的。如何修复此代码?

第一个文件:

one
two
three
four

第二个文件:

one
two
three
four
five

我的代码:

void diff(char* fileptr1, char* fileptr2)
{
  int maxlinelen=BUFF; //maximum line length buffer size

 /** string arrays pointers **/
  char *linebuffer1; 
  char *linebuffer2;      

  /** file pointers **/
  FILE *fp1;
  FILE *fp2;

  int line=0;    //line counter
  int counter=0; //identical flag

  linebuffer1=(char*)malloc(maxlinelen * sizeof(char*)); //memory allocation for linebuffers
  linebuffer2=(char*)malloc(maxlinelen * sizeof(char*));

  if((linebuffer1==NULL) || (linebuffer2==NULL))   //check memory allocating process
    {
     fprintf(stderr,"Command:diff :Memory allocating failed!\n");
      exit(1);
    }
  if(((fp1=fopen(fileptr1,"r"))!=NULL)&&((fp2=fopen(fileptr2,"r"))!=NULL))  //make sure both files open?
    {
      //read both files lines till end of line
      while(((fgets(linebuffer1,maxlinelen,fp1))!=NULL)&&((fgets(linebuffer2,maxlinelen,fp2))!=NULL)) 
    {
      while(strlen(linebuffer1)==maxlinelen-1) // perfect time for memory reallocating
        {
          maxlinelen*=DOUBLE;  //grow size
          linebuffer1=realloc(linebuffer1,maxlinelen * sizeof(char)); //reallocate memory to new size
          if(linebuffer1==NULL) //make sure allocation is succesfull
        {
          fprintf(stderr,"Command : diff :Memory reallocating failed for linebuffer1\n");
          exit(1);
        }
          fgets(linebuffer1+(maxlinelen/DIV-1),(maxlinelen/DIV)+1,fp1); //continue read line after reallocation
        }
      while(strlen(linebuffer2)==maxlinelen-1)
      {
        maxlinelen*=DOUBLE;
        linebuffer2=realloc(linebuffer2,maxlinelen * sizeof(char));
        if(linebuffer2==NULL)
          {
        fprintf(stderr,"Memory reallocating failed for linebuffer2\n");
        exit(2);
          }
        fgets(linebuffer2+(maxlinelen/DIV-1),(maxlinelen/DIV)+1,fp2);  //
      }
      line++;  //increae line counter


      if(strcmp(linebuffer1,linebuffer2)!=0)  //compare both line string arrays if not
             {
           printf("The files are different.The first difference is in line %d\n",line); //diff. here 
           exit(1);
         }

      if(strcmp(linebuffer1,linebuffer2)==0) //compare both line string arrays if same
             {
           counter++; //increase identical counter
         }
    }
      if(counter==line) //if identical counter equal total line
    {
      printf("The files are identical.\n");
    }
    }
  else {
    fprintf(stderr,"Command: diff :File  open failed!\n");

 }

  //fclose(fp1);fclose(fp2);
}
4

2 回答 2

0

我相信当任一文件结束时,您对读取文件循环的退出比较都会失败。这导致您不读取文件中的最后一行,从而增加行计数器变量。

您将需要辅助变量来记录每行的 fgets 的结果。

于 2012-11-13T02:24:34.423 回答
0

在这里,您使用“&&”作为两个文件的 fgets 结果。因此,当任一文件达到 EOF 时,比较就结束了。

你的第 5 行永远不会被比较。

while(((fgets(linebuffer1,maxlinelen,fp1))!=NULL)&&((fgets(linebuffer2,maxlinelen,fp2))!=NULL)

更改此 while 循环条件并修改相关逻辑将帮助您更正代码。

于 2012-11-13T02:22:22.330 回答