How to compare two text files in C

1 Answer

0 votes
#include <stdio.h>

int compareFiles(char file1[], char file2[]) {
	FILE *fp1 = fopen(file1, "r");
	FILE *fp2 = fopen(file2, "r");
	char ch1, ch2;

    do {
        ch1 = fgetc(fp1);
        ch2 = fgetc(fp2);
        
        if (ch1 != ch2)
            return 0;

    } while (ch1 != EOF && ch2 != EOF);

	return 1;
}


int main()
{
    char file1[100] = "d:\\data1.txt";
    char file2[100] = "d:\\data2.txt";

    if (compareFiles(file1, file2))
		puts("Equal");
	else
		puts("Not Equal");

    return 0;
}
 
   
   
   
/*
run:
   
Equal

*/

 



answered Jul 8, 2020 by avibootz

Related questions

1 answer 158 views
158 views asked Jun 9, 2018 by avibootz
1 answer 158 views
1 answer 117 views
117 views asked Jul 8, 2020 by avibootz
1 answer 142 views
1 answer 151 views
151 views asked Jun 10, 2018 by avibootz
1 answer 91 views
...