/*-------------------------------------------------------------------*/ /* cat1.c (The fscanf and fprintf functions) */ /*-------------------------------------------------------------------*/ #include /*-------------------------------------------------------------------*/ void copyFrom(FILE *psFile) /* Read all characters from *psFile, and write them to stdout. */ { char cChar; while (fscanf(psFile, "%c", &cChar) != EOF) fprintf(stdout, "%c", cChar); } /*-------------------------------------------------------------------*/ 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; }