C Program to Compare Two Binary Files

This C Program compares two binary files, printing the first byte position where they differ.

Here is source code of the C Program to compare two binary files, printing the first byte position where they differ. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Compare two Binary Files, Printing the First Byte 
  3.  * Position where they Differ
  4.  */
  5. #include <stdio.h>
  6.  
  7. void compare_two_binary_files(FILE *,FILE *);
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     FILE *fp1, *fp2;
  12.  
  13.     if (argc < 3)
  14.     {
  15.         printf("\nInsufficient Arguments: \n");
  16.         printf("\nHelp:./executable <filename1> <filename2>\n");
  17.         return;
  18.     }
  19.     else
  20.     {
  21.         fp1 = fopen(argv[1],  "r");
  22.         if (fp1 == NULL)
  23.         {
  24.             printf("\nError in opening file %s", argv[1]);
  25.             return;
  26.         }
  27.  
  28.         fp2 = fopen(argv[2], "r");
  29.  
  30.         if (fp2 == NULL)
  31.         {
  32.             printf("\nError in opening file %s", argv[2]);
  33.             return;
  34.         }
  35.  
  36.         if ((fp1 != NULL) && (fp2 != NULL))
  37.         {
  38.             compare_two_binary_files(fp1, fp2);
  39.         }
  40.     }
  41. }
  42.  
  43. /*
  44.  * compare two binary files character by character
  45.  */
  46. void compare_two_binary_files(FILE *fp1, FILE *fp2)
  47. {
  48.     char ch1, ch2;
  49.     int flag = 0;
  50.  
  51.     while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
  52.     {
  53.         /*
  54.           * character by character comparision
  55.           * if equal then continue by comparing till the end of files
  56.           */
  57.         if (ch1 == ch2)
  58.         {
  59.             flag = 1;
  60.             continue;
  61.         }
  62.         /*
  63.           * If not equal then returns the byte position
  64.           */
  65.         else
  66.         {
  67.             fseek(fp1, -1, SEEK_CUR);        
  68.             flag = 0;
  69.             break;
  70.         }
  71.     }
  72.  
  73.     if (flag == 0)
  74.     {
  75.         printf("Two files are not equal :  byte poistion at which two files differ is %d\n", ftell(fp1)+1);
  76.     }
  77.     else
  78.     {
  79.         printf("Two files are Equal\n ", ftell(fp1)+1);
  80.     }
  81. }

$ gcc file15.c
$ a.out /bin/chgrp /bin/chown
Two files are not equal :  byte poistion at which two files differ is 25
 
/*
 * Verify using cmp command
 */
$ cmp /bin/chgrp /bin/chown
/bin/chgrp /bin/chown differ: byte 25,  line 1
 
$ a.out a.out a.out
Two files are Equal
/*
 * Verify using cmp command
 */
$ cmp a.out a.out

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.