C program to compare two files contents

compare two files in c

In this article, I will write a C program to compare two files contents. A logic to compare two files character by character and line by line in C programming is easy. So the main attraction of this article to describe the steps to how to compare two files character by character and line by line in C programming.

Example,

Input 1 :

aticleworld1.txt contains
Hello Aticleworld.com

aticleworld2.txt contains
Hello Aticleworld.com

Output :
Both files are the same.


Input 2 :

aticleworld1.txt contains
Hello Aticleworld.com
Hi folks.

aticleworld2.txt contains
Hello Aticleworld.com

Output :
Both files are different
Line Number: 1
Position: 23

 

 

Logic to compare two files:

1.  Open two files using in read-only mode and get file pointer.
2.  Read the characters of both files using fgetc one by one until the end of the file.
3. If variable encounter new line then increments line number and reset position to zero.
4. If variables are not equal then set the flag (isContentMatch) and break the loop using the break statement.

If you want this tutorial in video format then here it is which explains the below program to how to compare two files using C.

Let see the C program to compare two files contents

 

#include <stdio.h>

//Maximum size of the array
#define MAX_SIZE  200

typedef struct
{
    int pos;
    int line;
} sMismatchingPos;

int isFileSame(const FILE *fp1, const FILE *fp2,sMismatchingPos *psMismatchPos)
{

    // pos and line to track of position of mismatch
    int pos = 0, line = 1;
    int ch1 =0, ch2 = 0;
    int isContentMatch = 0;

    // iterate loop until EOF
    do
    {
        //fetch character of both file
        ch1 = fgetc(fp1);
        ch2 = fgetc(fp2);

        ++pos;

        // if both variable encounters new
        // line then line variable is incremented
        // and pos variable is set to 0
        if ((ch1 == '\n') && (ch2 == '\n'))
        {
            ++line;
            pos = 0;
        }

        //update structure variable
        psMismatchPos->pos = pos;
        psMismatchPos->line = line;

        // if fetched data is not equal then
        // set the mismatched flag
        if(ch1!= ch2)
        {
            isContentMatch =1;
            break;
        }
    }
    while (ch1 != EOF && ch2 != EOF);

    //return flag status
    return isContentMatch;
}

int main()
{
    //file pointers
    FILE *fp1 = NULL;
    FILE *fp2 = NULL;
    //structure variable
    sMismatchingPos misMatchPos = {0};
    int isContentMatch = 0;

    // opening both file in read only mode
    fp1 = fopen("aticleworld1.txt", "r");
    fp2 = fopen("aticleworld2.txt", "r");

    //checking file open or not
    if (fp1 == NULL || fp2 == NULL)
    {
        printf("Error : Files not open");
        exit(1);
    }

    //if 1, then file mismatch
    isContentMatch = isFileSame(fp1, fp2,&misMatchPos);
    if(isContentMatch)
    {
        printf("Both files are different\n");
        //print line and pos where both file mismatch
        printf("Line Number : %d \n",misMatchPos.line);
        printf("Position : %d \n",misMatchPos.pos);
    }
    else
    {
        printf("Both files are same\n");
    }

    // closing both file
    fclose(fp1);
    fclose(fp2);

    return 0;
}

Output1:

When both files have the same content.

C program to compare two files contents

 

Output2:

When both files have different content.

C code to compare two files contents

 

 Code Analysis:

 

I have created a structure to store the mismatch position and the line.

typedef struct
{
    int pos;
    int line;
} sMismatchingPos;

 

In the above C program to compare two files contents, first, we opened to open both files which you want to compare. Here I am opening two files “aticleworld1.txt” and “aticleworld2.txt” and getting their file pointer. Using the if condition I am verifying that the file is opened successfully or not.

// opening both file in read only mode
fp1 = fopen("aticleworld1.txt", "r");
fp2 = fopen("aticleworld2.txt", "r");
//checking file open or not
if (fp1 == NULL || fp2 == NULL)
{
    printf("Error : Files not open");
    exit(1);
}

 

For comparing the file content, I have created a function isFileSame(). It takes file pointer of both file and structure pointer to get the mismatch position and line. In this function, I am using the fgetc function to read the file contents.

In this function, I am using the if condition to increment the line counter on changing of the new line.

int isFileSame(const FILE *fp1, const FILE *fp2,sMismatchingPos *psMismatchPos)
{
    // pos and line to track of position of mismatch
    int pos = 0, line = 1;
    int ch1 =0, ch2 = 0;
    int isContentMatch = 0;
    // iterate loop until EOF
    do
    {
        //fetch character of both file
        ch1 = fgetc(fp1);
        ch2 = fgetc(fp2);
        ++pos;
        // if both variable encounters new
        // line then line variable is incremented
        // and pos variable is set to 0
        if ((ch1 == '\n') && (ch2 == '\n'))
        {
            ++line;
            pos = 0;
        }
        //update structure variable
        psMismatchPos->pos = pos;
        psMismatchPos->line = line;
        // if fetched data is not equal then
        // set the mismatched flag
        if(ch1!= ch2)
        {
            isContentMatch =1;
            break;
        }
    }
    while (ch1 != EOF && ch2 != EOF);
    //return flag status
    return isContentMatch;
}

 

 

Related Article,



Leave a Reply

Your email address will not be published. Required fields are marked *