-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-6-compare.c
45 lines (35 loc) · 878 Bytes
/
7-6-compare.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 100
FILE *fopen_or_exit(char *prog, char *name)
{
FILE *fp;
if ((fp = fopen(name, "r")) == NULL) {
fprintf(stderr, "%s: can't open %s\n", prog, name);
exit(1);
} else {
return fp;
}
}
int main(int argc, char *argv[])
{
char *prog = argv[0];
if (argc != 3) {
fprintf(stderr, "%s expects exactly two file arguments\n", prog);
exit(1);
} else {
FILE *fp1, *fp2;
char line1[MAXLINE], line2[MAXLINE];
int i;
fp1 = fopen_or_exit(prog, argv[1]);
fp2 = fopen_or_exit(prog, argv[2]);
for (i = 0; fgets(line1, MAXLINE, fp1) && fgets(line2, MAXLINE, fp2); i++) {
if (strcmp(line1, line2)) {
printf("Difference on line %d:\n< %s---\n> %s", i, line1, line2);
exit(0);
}
}
printf("No differences found\n");
}
}