Thread: Comparing two files

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    73

    Comparing two files

    I am trying to compare two files through command line arguments. The result is that the program returns the line number at which the files differ based on the first byte that it encounters that differs. It should be working just fine, but it always returns a line value of 1. I cant figure out why...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    FILE *fp, *fr;
    int line = 1;
    char c, d;
    
    fp=fopen(argv[1], "r");
    fr=fopen(argv[2], "r");
    
    while(((c=fgetc(fp)) != EOF) || ((d=fgetc(fr)) != EOF)){
       if(c != d){
         printf("File differs at line %d", line);
         exit(0);
       }
       if(c == '\n'){
         line++;
       }
    }
          system("PAUSE");
          return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    while(((c=fgetc(fp)) != EOF) || ((d=fgetc(fr)) != EOF)){
    Since the first part of the logical or is true, the second part is "short circuited" and not performed. So you are most likely comparing a value of 'c' read from the first file with an indeterminate value of 'd'. This compares false and you exit immediately.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Ah, ok. I just moved the d=fgetc(fr); into the actual while loop. This works fine. Thanks.

  4. #4
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    or you could of done:

    Code:
    while(((c=fgetc(fp)) != EOF) && ((d=fgetc(fr)) != EOF)){

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Doesn't work like you would expect it to if you do that though.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    char c, d;
    
    while(((c=fgetc(fp)) != EOF)
    This is incorrect. You should've declared c and d as ints not chars. Read this to understand why
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    I've done a similiar function and I have problems.

    Code:
    int FileCompare (char nomebuffer[], char nomebackup[]){
        
        FILE *namebuffer, *namebackup;
        
        char buffer[MAX_KEY];
        char backup[MAX_KEY];
        int  lineabuffer = 0;
        
        namebuffer = fopen(nomebuffer, "rb");
        namebackup = fopen(nomebackup, "rb");
        
        while((fgets(buffer, MAX_KEY, namebuffer)) != NULL){
            
            ++lineabuffer;
            fgets(backup, MAX_KEY, namebackup);
            
            if (buffer != backup){
                 printf("Mismatch at line %d\n", lineabuffer);
                 printf("Buffer -> %sBackup -> %s\n",buffer, backup);
                 return 1;
            }//END IF
    
        }// END WHILE
    
            fclose(namebuffer);
            fclose(namebackup);       
            return 0;
        
    }//END FileCompare
    I have to compare 2 text files with written this :

    Code:
    NvCplDaemon
    RUNDLL32.EXE C:\WINDOWS\System32\NvCpl.dll,NvStartup
    AdslTaskBar
    rundll32.exe stmctrl.dll,TaskBar
    I copy the buffer file into a backup file, then I call FileCompare but it says there is allways a mismatch in line 1

    output:

    Mismatch at line 1:
    Buffer -> NvCplDaemon
    Backup -> NvCplDaemon
    I'm sure the lines are not different coz backup it is a copy of buffer previously done by:

    Code:
    CopyFile(nomebuffer, nomebackup, FALSE);
    any suggestion why the
    Code:
    if (buffer != backup)
    fails?
    Last edited by BianConiglio; 09-23-2004 at 04:36 AM.
    This forum is the best one I've ever seen. Great ppl, great coders

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by BianConiglio
    any suggestion why the
    Code:
    if (buffer != backup)
    fails?
    FAQ > How do I... (Level 1) > Comparing strings (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    BianConiglio, you've made 5 mistakes:
    1. Like Dave noted (buffer != backup) will compare adressess not strings
    2. You assume that namebuffer file is longer than (or as long as) namebackup, since you never check if it reached the end (look at EvBladeRunnervE post)
    3. You never close files if they aren't identical
    4. You never check if fopen() calls failed
    5. If you reading text, why open files with "rb"?

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    what an idiot!!!! thanx guys
    This forum is the best one I've ever seen. Great ppl, great coders

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Working code

    Code:
    void SicurezzaCompromessa(void){
    
        MessageBox( NULL,
                             "Error opening file.\n",
                             "Error:", 
                              MB_OK);
        exit(1);
    }//END SicurezzaCompromessa
    Code:
    int FileCompare (char nomebuffer[], char nomebackup[]){
        
        FILE *namebuffer, *namebackup;
        
        char buffer[MAX_KEY];
        char backup[MAX_KEY];
        int  lineabuffer = 0;
        
        namebuffer = fopen(nomebuffer, "r");
        namebackup = fopen(nomebackup, "r");
        
        if (namebuffer == NULL){
            
            SicurezzaCompromessa();
        }    
        
        if (namebackup == NULL){
            
            SicurezzaCompromessa();
        }  
        
        while((fgets(backup, MAX_KEY, namebackup )) != NULL){
            
            ++lineabuffer;
            fgets(buffer, MAX_KEY, namebuffer );
            
            if (strcmp(buffer,backup)!=0){
                 
                 printf("Mismatch at line %d\n", lineabuffer);
                 printf("Buffer -> %sBackup -> %s\n",buffer, backup);
              
                 fclose(namebuffer);
                 fclose(namebackup);
                 
                 return 1;
            }//END IF
        
          
        }// END WHILE
    
       fclose(namebuffer);
       fclose(namebackup);
              
       return 0;
        
    }//END FileCompare
    2. You assume that namebuffer file is longer than (or as long as) namebackup
    yes I know it before calling that function
    This forum is the best one I've ever seen. Great ppl, great coders

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fgets(buffer, MAX_KEY, namebuffer );
    Unfortunately, if the 2nd file is shorter than the first, you repeatedly compare the extra lines of file1 with the last line of file2.

    If both files have identical tails like

    one
    two
    three

    one
    two
    three
    three

    Your code will compare them equal.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    I hadn't noticed it before thanx

    anyway that situation is avoided in the main{} (I call that function only if buffer <= backup)

    anyway I will add (and post) an extra control so I can use this function everywhere safely
    Last edited by BianConiglio; 09-24-2004 at 08:09 AM.
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM