Advertisement
mdelatorre

Open files passed from command line

Feb 22nd, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.37 KB | None | 0 0
  1. // This code simulates how the Unix cat command works
  2.  
  3. #include <stdio.h>
  4.  
  5. main(int argc, char *argv[])
  6. {
  7. int i;
  8. FILE *fp;
  9. int c;
  10.  
  11. for(i = 1; i < argc; i++)
  12.     {
  13.     fp = fopen(argv[i], "r");
  14.     if(fp == NULL)
  15.         {
  16.         fprintf(stderr, "cat: can't open %s\n", argv[i]);
  17.         continue;
  18.         }
  19.  
  20.     while((c = getc(fp)) != EOF)
  21.         putchar(c);
  22.  
  23.     fclose(fp);
  24.     }
  25.  
  26. return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement