Quipex

lab2

Mar 26th, 2017
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <assert.h>
  4. #include <stdlib.h>
  5.  
  6. void printfile(char *file);
  7. void invalidArgs();
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     if (argc==1)
  12.     {
  13.         invalidArgs();
  14.     }
  15.     int i;
  16.     int keys=0;
  17.     for (int i = 1; i < argc; ++i)
  18.     {
  19.         if (argv[i][0]!='-') { printfile(argv[i]); }
  20.         else keys++;
  21.     }
  22.     if (keys==argc-1)
  23.     {
  24.         invalidArgs();
  25.     }
  26.     exit(0);
  27.     return 0;
  28. }
  29.  
  30. void printfile(char *file){
  31.     assert(file!=NULL);
  32.     FILE *fp = fopen(file, "rb");
  33.     if (!fp)
  34.     {
  35.         fprintf(stderr, "Can't open input file %s!\n", file);
  36.         exit(ENOENT);
  37.     }
  38.     int buff;
  39.     while ((buff=fgetc(fp)) != EOF){
  40.         printf("%c", buff);
  41.     }
  42.     fclose(fp);
  43. }
  44.  
  45. void invalidArgs(){
  46.     fprintf(stderr, "usage: printfiles file1 [file2 ...]\n    file1, file2, etc.: files to print\n");
  47.     exit(EINVAL);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment