dtung

cat.c

Jan 14th, 2022 (edited)
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. // source code from K&R 2nd page 162
  2. #include <stdio.h>
  3.  
  4. /* cat */
  5. int main(int argc, char *argv[])
  6. {
  7.     FILE *fp;
  8.     void filecopy(FILE *, FILE *);
  9.  
  10.     if (argc == 1)
  11.         filecopy(stdio, stdout);
  12.     else
  13.         while (--argc > 0)
  14.             if ((fp = fopen(*++argv, "r")) == NULL) {
  15.                 printf("cat: can't open %s\n", *argv);
  16.                 return 1;
  17.             } else {
  18.                 filecopy(fp, stdout);
  19.                 fclose(fp);
  20.             }
  21.     return 0;
  22. }
  23.  
  24. /* filecopy */
  25. void filecopy(FILE *ifp, FILE *ofp)
  26. {
  27.     int c;
  28.  
  29.     while ((c = getc(ifp)) != EOF)
  30.         putc(c, ofp);
  31. }
Add Comment
Please, Sign In to add comment