/* Compares two files, printing the first line where they differ */ #include #define MAXLINE 1000 int main(int argc, char *argv[]) { char line1[MAXLINE], line2[MAXLINE]; char *prgname = argv[0]; FILE *fp1, *fp2; int count; if (argc != 3) { fprintf(stderr, "%s: error: invalid number of arguments\n", prgname); return 1; } if ((fp1 = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: error: unable to open file: %s\n", prgname, argv[1]); return 1; } if ((fp2 = fopen(argv[2], "r")) == NULL) { fprintf(stderr, "%s: error: unable to open file: %s\n", prgname, argv[2]); return 1; } count = 0; while (fgets(line1, MAXLINE, fp1) && fgets(line2, MAXLINE, fp2)) { count++; if (strcmp(line1, line2)) { printf("First difference between files occurs at line %d\n", count); printf("\nFile 1 - %s:\n", argv[1]); printf("%s", line1); printf("\nFile 2 - %s: \n", argv[2]); printf("%s", line2); return 0; } } if (feof(fp1) && feof(fp2)) printf("Files are identical\n"); else if (feof(fp1)) printf("All of %s is identical with the beginning of %s\n", argv[1], argv[2]); else if (feof(fp2)) printf("All of %s is identical with the beginning of %s\n", argv[2], argv[1]); return 2; }