/*-------------------------------------------------------------------*/ /* cat3.c (The fgetc and fputc functions) */ /*-------------------------------------------------------------------*/ #include /*-------------------------------------------------------------------*/ void copyFrom(FILE *psFile) /* Read all characters from *psFile, and write them to stdout. */ { int iChar; while ((iChar = fgetc(psFile)) != EOF) fputc(iChar, stdout); } /*-------------------------------------------------------------------*/ int main(int argc, char *argv[]) /* If argc == 1, then read all characters from stdin, and write them to stdout. Otherwise read all characters 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; }