/*-------------------------------------------------------------------*/ /* cat2.c (The fgets and fputs functions) */ /*-------------------------------------------------------------------*/ #include #define MAX_LINE_LENGTH 256 /*-------------------------------------------------------------------*/ void copyFrom(FILE *psFile) /* Read all lines from *psFile, and write them to stdout. */ { char pcBuffer[MAX_LINE_LENGTH]; while (fgets(pcBuffer, MAX_LINE_LENGTH, psFile) != NULL) fputs(pcBuffer, 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. */ { 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; }