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.
/*
* C Program to Compare two Binary Files, Printing the First Byte
* Position where they Differ
*/
#include <stdio.h>
void compare_two_binary_files(FILE *,FILE *);
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
if (argc < 3)
{
printf("\nInsufficient Arguments: \n");
printf("\nHelp:./executable <filename1> <filename2>\n");
return;
}
else
{
fp1 = fopen(argv[1], "r");
if (fp1 == NULL)
{
printf("\nError in opening file %s", argv[1]);
return;
}
fp2 = fopen(argv[2], "r");
if (fp2 == NULL)
{
printf("\nError in opening file %s", argv[2]);
return;
}
if ((fp1 != NULL) && (fp2 != NULL))
{
compare_two_binary_files(fp1, fp2);
}
}
}
/*
* compare two binary files character by character
*/
void compare_two_binary_files(FILE *fp1, FILE *fp2)
{
char ch1, ch2;
int flag = 0;
while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
{
/*
* character by character comparision
* if equal then continue by comparing till the end of files
*/
if (ch1 == ch2)
{
flag = 1;
continue;
}
/*
* If not equal then returns the byte position
*/
else
{
fseek(fp1, -1, SEEK_CUR);
flag = 0;
break;
}
}
if (flag == 0)
{
printf("Two files are not equal : byte poistion at which two files differ is %d\n", ftell(fp1)+1);
}
else
{
printf("Two files are Equal\n ", ftell(fp1)+1);
}
}
$ 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.
Related Posts:
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Check C Books
- Practice BCA MCQs
- Apply for C Internship