/*------------------------------------------------------------------*/ /* cat2.c */ /* Author: Bob Dondero */ /* The fgets() and fputs() functions */ /*------------------------------------------------------------------*/ #include enum {MAX_LINE_LENGTH = 256}; /*------------------------------------------------------------------*/ static void copyFrom(FILE *psFile) /* Read all lines from *psFile, and write them to stdout. */ { char acBuffer[MAX_LINE_LENGTH]; while (fgets(acBuffer, MAX_LINE_LENGTH, psFile) != NULL) fputs(acBuffer, stdout); } /*------------------------------------------------------------------*/ int main(int argc, char *argv[]) /* If argc == 1, then read all lines from stdin, and write them to stdout. Otherwise read all lines from the files named argv[1]...argv[argc-1], and write them to stdout. Return 0. */ { int i; if (argc == 1) copyFrom(stdin); else for (i = 1; i < argc; i++) { FILE *psFile; psFile = fopen(argv[i], "r"); if (psFile == NULL) fprintf(stderr, "%s: %s: No such file or directory\n", argv[0], argv[i]); else { copyFrom(psFile); fclose(psFile); } } return 0; }