diff.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * diff.c prints the first line that differs from two files.
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. const int MAX = 64;
  7. struct filesLines {
  8. FILE *fp1;
  9. FILE *fp2;
  10. char line1[64];
  11. char line2[64];
  12. };
  13. /*
  14. * Get the next two lines from each file;
  15. */
  16. int getlines (struct filesLines flines)
  17. {
  18. flines.line1 = fgets (flines.line1, MAX, flines.fp1);
  19. flines.line2 = fgets (flines.line2, MAX, flines.fp1);
  20. if (flines.line1 == NULL || flines.line2 == NULL)
  21. return 0;
  22. else
  23. return 1;
  24. }
  25. int
  26. main (int argc, char *argv[])
  27. {
  28. struct filesLines flines;
  29. /* while each file has another line */
  30. if (argc != 3)
  31. {
  32. printf("Usage: diff FILE1 FILE2\n");
  33. return 1;
  34. }
  35. /*
  36. * open the two specified files
  37. */
  38. flines.file1 = fopen(argv[1], "r");
  39. flines.file2 = fopen(argv[2], "r");
  40. while (getlines(flines)) {
  41. printf("File1\n %s\n", flines.line1);
  42. printf("File2\n %s\n", flines.line2);
  43. /* if the lines differ, print out the first line that differs */
  44. if (strcmp (flines.line1, flines.line2)) {
  45. printf("File1\n %s\n", flines.line1);
  46. printf("File2\n %s\n", flines.line2);
  47. }
  48. return 0;
  49. }
  50. }