C Program to Compare Two Data files

This page has a C Program to Compare Two Data files. It is clear that, this program will ask names of two existing files as input (these two data files must be created earlier either through notepad or ms-word or by any other way).Then, the program will compare the contents of the given two data files. If both files contains same contents (information), the program will display the output as the two files are Identical. If both files are having different contents, the program will display the output as the two files are not Identical. The below given C program is used to Compare Two Data files. Kindly check out the program to Compare Two Data files.

How will ask to Compare Two Data files

The questions to copy the content of one file to another file can be asked as, 1). C program to Compare any two Data files. 2). C Program to Compare two files. 3). C Program to Compare one Data file with other. 4). C Program to Compare any given two Data files. 5). C program to Compare the contents of any two Data files. 6). C program to Compare the contents of two Data files.

Program to Compare any two Data files

#include<stdio.h>

int main() {

FILE *fpointer1, *fpointer2;

char filechar1, filechar2;

char nam1[20], nam2[20];

clrscr();

printf("

Give the first file name:");

gets(nam1);

printf("

Give the second file name:");

gets(nam2);

fpointer1 = fopen(nam1, "r");

fpointer2 = fopen(nam2, "r");

if (fpointer1 == NULL)

printf("

The file %s has no content to compare !", nam1);

else if (fpointer2 == NULL)

printf("

The file %s has no content to compare !", nam2);

else

{

do {

filechar1 = getc(fpointer1);

filechar2 = getc(fpointer2);

}while ((filechar1 != EOF) && (filechar2 != EOF) && (filechar1 == filechar2));

if (filechar1 == filechar2)

printf("

The Two Files are identical");

else if (filechar1 != filechar2)

printf("

The Two Files are Not identical");

fcloseall();

}

getch();

return (0);

}

Sample Outputs: Create two Text (data) files either using any Text editor (for example: notepad or notepad++ or Ms-word) before to run the program. If you do not create the two text files for using it in the program with 'r' mode, the program will give the output as "This file has no content to compare !!