2012-11-13 4 views
-2

내 코드의 한 조건이 작동하지 않습니다. 이 조건은 첫 번째 파일이 4 행이되고 두 번째 파일이 5 행이되는 경우입니다. 두 파일의 첫 번째 4 줄은 동일하지만 두 번째 파일의 다섯 번째 줄은 다를 수도 있고 같을 수도 있습니다. 제 출력물에는 "5 번째 줄에는 다른 것이 있습니다"라고 표시되어야하지만,이 파일들이 동일하다는 것을 의미합니다. 이 코드를 수정하려면 어떻게해야합니까?2 텍스트 파일 간의 차이점을 찾으십시오

첫 번째 파일 :

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

다른 사람에게 CompSci 숙제 질문을하는 것처럼 보입니다. –

+0

가장 큰 피로감은 들여 쓰기가 모두 펑크 난다는 것입니다. –

+0

편집 해 주셔서 감사합니다. – ccc

답변

0

, 당신은 "사용 & & "두 파일의 fgets 결과. 두 파일 중 하나가 EOF에 도달하면 비교가 끝났습니다.

다섯 번째 줄은 절대 비교되지 않습니다.

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

루프 상태와 수정 상대 논리를 수정하면 코드를 수정하는 데 도움이됩니다.

+0

ok 감사합니다. 나는 여기에서 문제를 안다. 그러나 나는이 선을 어떻게 바꿀 수 있는지에 관해 계산할 수 없었다. 다른 동안있을 수 있습니까? – ccc

+0

while 루프에서 플래그를 사용합니다. 플래그가 true 인 경우 그 중 하나가 NULL이더라도 행을 계속 비교합니다. 두 파일 끝이 플래그를 false로 설정할 때. @ccc – oyss

+0

이 루프는 불가능한 것처럼 보입니다. 나는 그것에 종사하고있다. – ccc

0

두 파일이 끝나면 읽기 파일 루프에 대한 종료 비교가 실패합니다. 따라서 파일의 마지막 줄을 읽지 않아서 줄 카운터 변수가 증가합니다.

각 행에 대한 fgets 결과를 기록하려면 helper vars가 필요합니다.

+0

그 또 다른 2 개의 배열처럼? 모든 요소를 ​​비교합니까? – ccc

관련 문제