-
Notifications
You must be signed in to change notification settings - Fork 0
/
filecmp.c
49 lines (41 loc) · 1020 Bytes
/
filecmp.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
43
44
45
46
47
48
49
/* Exercise 7-6. Write a program to compare two files, printing the first line
* where they differ. */
#include "stdio.h"
#include "string.h"
#define MAXLINELEN 10000
#define DEBU
int main(int argc, char **argv) {
#ifdef DEBUG
#include "unistd.h"
argc = 3;
const char *argv_[3] = {
"./filecmp",
"cat.c",
"cfh.c"
};
argv = argv_;
char buffer[100];
getcwd(buffer, 100);
printf("1111111%s", buffer);
#endif
const char *usage = "usage: filecmp FILE0 FILE1.";
char line[2][MAXLINELEN];
char *prog = *argv;
FILE *fp[2];
if (argc != 3) {
fprintf(stderr, "%s\n", usage);
return 1;
}
while (--argc)
if ( !(fp[2-argc] = fopen(*++argv, "r")) ) {
fprintf(stderr, "%s: failed to read file %s.\n", prog, *argv);
return 1;
}
while ((fgets(line[0], MAXLINELEN, fp[0])) &&
(fgets(line[1], MAXLINELEN, fp[1])))
if (strcmp(line[0], line[1])) {
printf("First line where they differ:\n%s%s", line[0], line[1]);
return 0;
}
return 0;
}