正如标题所说,我需要比较 2 个文件。如果出现一行,这两个文件之间不一样,请打印该行(来自两个文件)。代码给出了不发送错误。我不能似乎能够在我的代码中找到错误。
#include <stdio.h>
#include <string.h>
#define MAX 1000
int main(int argc, char *argv[]) {
char c1, c2;
char s1[MAX], s2[MAX];
char *p1;
char *p2;
FILE *fp1;
FILE *fp2;
fp1 = fopen(argv[1], "r");
fp2 = fopen(argv[2], "r");
p1 = s1;
p2 = s2;
if (argc != 3) {
printf("Wrong use of program \n ");
return -1;
}
if (fp1 == NULL || fp2 == NULL) {
printf("One or both of the files can't be used \n ");
return -1;
}
while ((c1 = getc(fp1)) != EOF || (c2 = getc(fp2)) != EOF) {
*p1 = c1;
*p2 = c2;
p1++;
p2++;
for (c1 = getc(fp1); c1 != '\n'; p1++) {
*p1 = c1;
}
*p1 = '\0';
for (c2 = getc(fp2); c2 != '\n'; p2++) {
*p2 = c2;
}
*p2 = '\0';
if (!(strcmp(s1, s2))) {
printf("%s \n ", s1);
printf("%s \n ", s2);
return 0;
}
}
return 0;
}