C Programming File Handling | comparing two files in *files

Write a Modular C Programming code for Comparing two files on files*

*The file should be saved in (file1.txt & file2.txt) this Format in the location of this c file.
Below is the content of File:

FactsPrime

Refer To Notes:  Notes: File Handling in C programming | Operations in Files

CODE:

#include<stdio.h>
#include<stdlib.h>
void compareFiles(FILE *fp1, FILE *fp2)
{

    char ch1 = getc(fp1);
    char ch2 = getc(fp2);

    int error = 0;
    while (ch1 != EOF && ch2 != EOF)
    {
        if (ch1 != ch2)
        {
            error++;
            break;
        }
        ch1 = getc(fp1);
        ch2 = getc(fp2);
    }
    if(error>0)
        printf("Both the file content are not same\n ");
    else
        printf("Both the file content are same\n ");
}

main()
{
FILE *fp1;
FILE *fp2;
    fp1 = fopen("file1.txt", "r");
    fp2 = fopen("file2.txt", "r");

    if (fp1 == NULL || fp2 == NULL)
    {
       printf("Error : Files not open");
       exit(0);
    }

    compareFiles(fp1, fp2);
    fclose(fp1);
    fclose(fp2);
}

OUTPUT

It will Display Accordingly:
Both the file content are not same
or
Both the file content are same

Process returned 0 (0x0)   execution time : 369.409 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, and MISC on the below page:

Top 100+ C Programming codes – KLE Technological University

 

 







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!