Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/Ex_7.4v2.c
1240 views
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
//compare two files, printing the first line where they differ
5
int main(int argc, char *argv[]){
6
FILE *fp1, *fp2;
7
void filecmp(FILE *, FILE *);
8
char *prog = argv[0];
9
if(argc == 3){
10
if((fp1 = fopen(argv[1], "r")) ==NULL || (fp2 = fopen(argv[2], "r")) ==NULL){
11
fprintf(stderr, "%s: can't open %s\n", prog, argv[1]);
12
exit(1);
13
}
14
else{
15
filecmp(fp1, fp2);
16
fclose(fp1);
17
fclose(fp2);
18
}
19
}
20
else{
21
printf("%s", "Please enter two file names");
22
}
23
exit(0);
24
}
25
26
void filecmp(FILE *f1, FILE *f2){
27
size_t size = 100;
28
char *string;
29
char *string1;
30
31
while(!feof(f1) && !feof(f2)){
32
string = (char *) malloc(size);
33
getline(&string, &size, f1);
34
string1 = (char *) malloc(size);
35
getline(&string1, &size, f2);
36
if(strcmp(string, string1)!= 0)
37
{
38
printf("%s", string);
39
printf("%s", string1);
40
return;
41
}
42
}
43
}
44
45