12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /*
- * diff.c prints the first line that differs from two files.
- */
- #include <stdio.h>
- #include <string.h>
- const int MAX = 64;
- struct filesLines {
- FILE *fp1;
- FILE *fp2;
- char line1[64];
- char line2[64];
- };
- /*
- * Get the next two lines from each file;
- */
- int getlines (struct filesLines flines)
- {
- flines.line1 = fgets (flines.line1, MAX, flines.fp1);
- flines.line2 = fgets (flines.line2, MAX, flines.fp1);
- if (flines.line1 == NULL || flines.line2 == NULL)
- return 0;
- else
- return 1;
- }
- int
- main (int argc, char *argv[])
- {
- struct filesLines flines;
- /* while each file has another line */
- if (argc != 3)
- {
- printf("Usage: diff FILE1 FILE2\n");
- return 1;
- }
- /*
- * open the two specified files
- */
- flines.file1 = fopen(argv[1], "r");
- flines.file2 = fopen(argv[2], "r");
- while (getlines(flines)) {
- printf("File1\n %s\n", flines.line1);
- printf("File2\n %s\n", flines.line2);
- /* if the lines differ, print out the first line that differs */
- if (strcmp (flines.line1, flines.line2)) {
- printf("File1\n %s\n", flines.line1);
- printf("File2\n %s\n", flines.line2);
- }
- return 0;
- }
- }
|