https://www.bybit.com/register?affiliate_id=38000&group_id=52135&group_type=1
[856x508] Get $90 Rewards

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