Binary comparison from 2 files

This is a discussion on Binary comparison from 2 files within the C Programming forums, part of the General Programming Boards category; Howdy all! Im trying to write a simple program which opens two binary files and does a binary AND for ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    Binary comparison from 2 files

    Howdy all!

    Im trying to write a simple program which opens two binary files and does a binary AND for every byte in the files. If the bytes compared are not equal I would like to increment a counter and return the number of bytes not equal in the files.

    Look at this code, could anyone tell me what is wrong

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      FILE *file1, *file2;
    
      char c1, c2;
      unsigned long int errors = 0;
      unsigned long int count = 0;
      
      printf("\n");
      while(--argc > 0)
        printf("%s\n", argv[argc]);
    
      file1 = fopen(argv[1], "rb");
      file2 = fopen(argv[2], "rb");
    
      if(file1 == NULL)
        {
          printf("Error: can't open file number one.\n");
        }
      if(file2 == NULL)
        {
          printf("Error: can't open file number two.\n");
        }
      else
        {
          while(1)
    	{
    	  fread(&c1, sizeof(char), 1, file1);
    	  fread(&c2, sizeof(char), 1, file2);
    
    	  if(c1 != EOF)
    	    {
    	      if((c1 & c2) != 1)
    		errors++;
    
    	      count++;
    	    }
    	  else
    	    {
    	      break;
    	    }
    	}
          fclose(file1);
          fclose(file2);
          printf("\nResult: %lu bits compared. %lu equal bits found\n\n", count, count-errors);
        }
      return 1;
    }
    The files I'm using are of equally size.

    Thanks for you help,
    Filly

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a re-do that does about what you want. It uses two very small text files, and correctly determines that one letter is different.

    I wouldn't try de-bugging from the command line, if possible.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      FILE *file1, *file2;
    
      char c1, c2, str1[19], str2[19];
      unsigned long int errors = 0;
      unsigned long int count = 0;
      int file1ok, file2ok;
    
      printf("\n\n");
      /* while(--argc > 0)
          printf("&#37;s\n", argv[argc]);
      */
      file1 = fopen("compare1.txt", "rb");
      file2 = fopen("compare2.txt", "rb");
    
      if(file1 == NULL)
      {
        printf("Error: can't open file number one.\n");
      }
      if(file2 == NULL)
      {
        printf("Error: can't open file number two.\n");
      }
      else
      {
         while(1)
         {
            file1ok = fread(&c1, sizeof(char), 1, file1);
            file2ok = fread(&c2, sizeof(char), 1, file2);
          
    	if(file1ok && file2ok)
    	{
    	   if((c1 ^ c2) != 0)
    	      errors++;
    	   count++;
               printf(" %c",  c1);
    	}
    	else
                break;
         } /* end of while */
    
         fclose(file1);
         fclose(file2);
         printf("\nResult: %lu bits compared. %lu equal bits found\n\n", count, count-errors);
    
      } /* end of else */
    
       system("pause");
       return 0;
    }
    The file contained just this:

    Now is the time for - compare1.txt
    Now *s the time for - compare2.txt

    The file handling logic is still in need of some work, as dwk mentions below. Indentation problem is fixed - it was done on a different compiler from what I usually use, on a different computer. Since FC (file compare), is part of every windows and dos OS, and does a better job, I didn't worry about the portability of this learning tool.
    Last edited by Adak; 01-15-2008 at 05:54 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    2
    Thank you so much. Exactly what I needed.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You do realize that if file1 was opened, but file2 couldn't be, then you never close file1. And this
    Code:
    if(file2 == NULL)
    should be else if, otherwise, when file1 couldn't be opened, but file 2 could be, you'll try to use the NULL file1.

    Also, sizeof(char) is always 1. And system("pause"), besides being unportable, is in stdlib.h. And your indentation makes the else look like it belongs to a different if than it does . . . .

    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by dwks View Post
    You do realize that if file1 was opened, but file2 couldn't be, then you never close file1. And this
    Code:
    if(file2 == NULL)
    should be else if, otherwise, when file1 couldn't be opened, but file 2 could be, you'll try to use the NULL file1.

    Also, sizeof(char) is always 1. And system("pause"), besides being unportable, is in stdlib.h. And your indentation makes the else look like it belongs to a different if than it does . . . .

    Yes, I pointed that out to him in an earlier post (about the wrong file handling logic). I deleted that post after I put up the working program of his, because it was too critical. I'll add that as an edit to the program post, however, and fix the indentation. It wasn't meant to be a "good program". It was meant to remain the OP's program, with a few touch-ups on the part that didn't work.

    As for system("pause"); it was added just for a time-saver on my part, and for clarity for him. It works, as listed.
    Last edited by Adak; 01-15-2008 at 05:46 PM.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    I would rewrite

    Code:
    while(1)
         {
            file1ok = fread(&c1, sizeof(char), 1, file1);
            file2ok = fread(&c2, sizeof(char), 1, file2);
          
    	if(file1ok && file2ok)
    	{
    	   if((c1 ^ c2) != 0)
    	      errors++;
    	   count++;
               printf(" %c",  c1);
    	}
    	else
                break;
         } /* end of while */
    into

    Code:
    while(fread(&c1, sizeof(char), 1, file1) && 
             fread(&c2, sizeof(char), 1, file2)
    {   
        if((c1 ^ c2) != 0)
          errors++;
       count++;
    
       printf(" %c",  c1);
    } /* end of while */

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I prefer:
    Code:
    	   if(c1 != c2)
    	      errors++;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fseek and binary files
    By kambrian in forum C Programming
    Replies: 2
    Last Post: 02-06-2006, 03:20 PM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  3. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  4. Making Binary Output to files using VC++ 6's "fstream"
    By Perica in forum C++ Programming
    Replies: 5
    Last Post: 02-05-2003, 12:48 AM
  5. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 12:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21