Write a C program that: Something that is frequently useful is to compare two fi
ID: 3774699 • Letter: W
Question
Write a C program that:
Something that is frequently useful is to compare two files to see if they are identical. Write a program that: Reads in two text files. The text files consist of 100 integers arranged in 10 rows of 10. The program should should compare each item in the two files, checking to see if all the corresponding items are the same. If they identical, the program should report that conclusion. If they are not identical, the program should report how many of the 100 items were not the same. In other words, how many mismatches were there? You will be given four files, test 1. dat", "test2.dat", "test3.dat", and "test4.dat" These will be sent to you by email. You should run your program for each possible combination of files. Demonstrate your program to your lab instructor.Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
FILE *fp1, *fp2;
char name[100];
int i,j;
int x,y;
int mismatch = 0;
printf("Enter the location of file 1: ");
scanf("%s",name);
fp1 = fopen(name, "r");
printf("Enter the location of file 2: ");
scanf("%s",name);
fp2 = fopen(name, "r");
for(i=0;i<10&&fp1!=NULL&&fp2!=NULL;i++){
for(j=0;j<10&&fp1!=NULL&&fp2!=NULL;j++){
fscanf(fp1, "%d", &x);
fscanf(fp2, "%d", &y);
if(x!=y) {
mismatch ++;
}
}
}
if(mismatch == 0) {
printf("The files are identical!");
} else {
printf("The files are not identical. %d numbers have mismatched.",mismatch);
}
return(1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.