lolamontes69

K+R Exercise7_6

Sep 25th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. /* Write a program to compare two files, printing the first line where they differ
  2.  *
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define MAXCHAR 100
  8.  
  9. int filecmp1(FILE *iop, FILE *iop1);
  10. char *fgets1(char *s, int n, FILE *iop);
  11. int fputs1(char *s, FILE *iop);
  12. int strcmp1(char *s, char *t);
  13.  
  14. /* compare the lines of two files if a difference is found output the line and quit */
  15. main(int argc, char *argv[])
  16. {
  17.     FILE *fp;
  18.     FILE *fp1;
  19.  
  20.     char *prog = argv[0];
  21.     int ret=0;
  22.  
  23.     if(argc <= 2) {
  24.         fprintf(stderr, "%s: not enough args\n", prog);
  25.         exit(2);
  26.     } else if(argc == 3) {
  27.         if((fp = fopen(*++argv, "r")) == NULL) {   /* first open the FILE object */
  28.             fprintf(stderr, "%s: can't open %s\n", prog, *argv);
  29.             exit(1);
  30.         } else if((fp1 = fopen(*++argv, "r")) == NULL) { /* open the next FILE object */
  31.             fprintf(stderr, "%s: can't open %s\n", prog, *argv);
  32.             exit(1);
  33.         } else {
  34.             ret = filecmp1(fp,fp1);
  35.         }
  36.     }
  37. }
  38.  
  39. /* filecmp1: compare the lines of two files if a difference is found
  40.  *           output the line and quit */
  41. int filecmp1(FILE *iop, FILE *iop1)
  42. {
  43.     int ret=0, lineno=0;
  44.  
  45.     char lineA[MAXCHAR];
  46.     char lineB[MAXCHAR];
  47.  
  48.     while(fgets1(lineA, MAXCHAR, iop)) {
  49.         fgets1(lineB, MAXCHAR, iop1);
  50.         ret = strcmp1(lineA, lineB);
  51.         lineno+=1;
  52.         if(ret!=0) {
  53.             printf("line %d: %s",lineno, lineB);
  54.             break;
  55.         }
  56.     } if(ret==0) {
  57.         if(fgets1(lineB, MAXCHAR, iop1)) {
  58.             puts("Note: file2 is longer than file1!");
  59.         } puts("The lines in file2 match those in file1");
  60.     } return ret;
  61. }
  62.  
  63. /* fgets1: get at most n chars from iop */
  64. char *fgets1(char *s, int n, FILE *iop)
  65. {
  66.     register int c;
  67.     register char *cs;
  68.  
  69.     cs = s;
  70.     while(--n > 0 && (c = getc(iop)) != EOF)   /* get up to n characters from one line of iop */
  71.         if((*cs++ = c) == '\n')                /* quit at '\n' cs is at the same address as s  */
  72.             break;
  73.     *cs = '\0';
  74.     return (c == EOF && cs == s) ? NULL : s;   /* ternary operation */
  75. }
  76.  
  77. /* fputs1: put string s on file iop */
  78. int fputs1(char *s, FILE *iop)
  79. {
  80.     int c;
  81.  
  82.     while(c = *s++)
  83.         putc(c, iop);
  84.     return ferror(iop) ? EOF : 0;
  85. }
  86.  
  87. /* strcmp1: return <0 if s<t, O if s==t, >0 if s>t */
  88. int strcmp1(char *s, char *t)
  89. {
  90.     int i;
  91.  
  92.     for( ; *s == *t; s++, t++)
  93.         if(*s =='\0')
  94.             return 0;
  95.     return *s - *t;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment