Question
Write a c program

CH-12 TEXT FILES EXERCISE 12-12 Write a program to read two files of the same length, and determine if they are equal. For ex
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int areTheyEqual(FILE *fp1, FILE *fp2)
{
//getting characters in two files in two char variables a and b
char a=getc(fp1);
char b=getc(fp2);
//running loop until atleast one of them reaches end of file
while(a!=EOF && b!=EOF)
{

if(a!=b) return 0;//if two characters at any time are not equal returning 0
//again giving new values to a and b
a=getc(fp1);
b=getc(fp2);
}
//if one variable reaches end of line and other doesnot then the two files are not equal
if(a!=EOF &&b!=EOF) return 0;
//if it came here then the two files are equal so returning 1
return 1;
}
int main() {

//we shall open files in read only format
   FILE *fp1 = fopen("first.txt", "r");
FILE *fp2 = fopen("second.txt", "r");
//checking if any pointers are NULL
if (fp1 == NULL || fp2 == NULL)
{
printf("files did not opened");
exit(0);
}
//if the function returns 1 then they are equal else they are not equal
if(areTheyEqual(fp1,fp2)) printf("they are equal\n");
else printf("they are not equal\n");
//closing the two files
fclose(fp1);
fclose(fp2);
return 0;

}

#include<stdio.h> #include<string.h> #include(stdlib.h> 2 5 int areTheyEqual (FILE fp1, FILE fp2) 6 //getting characters in t5 int areTheyEqual (FILE fp1, FILE fp2) 6 //getting characters in two files in two char variables a and b char a-getc(fp1); chey are not equal process returned θ (exe) execution time : 4.578 s ress any key to continue Projects iew: Curren Search: Symfirst - Notepad File Edit Format View Help 12: abc 45 bye!second - Notepad File Edit Format View Help 123 abc 55 bye! ераif you are satisfied please upvote or if you have any doubts post them in comment section happy learning

Add a comment
Know the answer?
Add Answer to:
Write a c program CH-12 TEXT FILES EXERCISE 12-12 Write a program to read two files of the same length, and determine if they are equal. For example: if one of the file has the content 123 abc 45...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT