【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
#include <stdio.h>;
#include <string.h>;
#define MAXLINE 100

void diff(char *a, char *b);
int main(int argc, char *argv[])
{
if(3 != argc){
printf("参数不正确\n");
return -1;
}

diff(argv[1], argv[2]);

return 0;
}

void diff(char *a, char *b)
{
FILE *afp;
FILE *bfp;
char aline[MAXLINE];
char bline[MAXLINE];

if((afp = fopen(a, "r")) == NULL){
printf("Error: not file %s", a);
return;
}

if((bfp = fopen(b, "r")) == NULL){
printf("Error: not file %s", b);
return;
}

while((fgets(aline, MAXLINE, afp) == aline) &amp;&amp; (fgets(bline, MAXLINE, bfp) == bline)){
if(strcmp(aline, bline) != 0){
printf("%s:%s", a, aline);
printf("%s:%s", b, bline);
break;
}
}
fclose(afp);
fclose(bfp);
}