Search This Blog

Translate

C program to compares two binary files, printing the first byte position where they differ

Write a C program to compares two binary files, printing the first byte position where they differ solve mca c programming Exam question paper 2013-2014, 

how to compare two file using pointer function using c programming example with source code, c file handling program using binary files

Write a C program to compares two binary files, printing the first byte position where they differ.

  


#include 
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  \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);
        }
    }
}
 
void compare_two_binary_files(FILE *fp1, FILE *fp2)
{
    char ch1, ch2;
    int flag = 0;
 
    while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
    {
        if (ch1 == ch2)
        {
            flag = 1;
            continue;
        }
        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);
    }
}

C Program example List

Google+ Badge