#include <stdio.h>
#include <stdlib.h>
void filecopy(FILE *ifp, FILE *ofp) {
int c;
while ((c = getc(ifp)) != EOF) { // #define EOF -1
putc(c, ofp);
}
}
int main(int argc, char *argv[]) { // 指针数组
FILE *fp;
void filecopy(FILE *, FILE *);
char *prog = argv[0];
if (argc == 1) { // 只输入函数名字时
filecopy(stdin, stdout);
}
else {
while (--argc) { // 连续输出多个文件,循环argc-1次
if ((fp = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "%s: can't open %s\n", prog, *argv);
exit(1);
// printf("cat: can't open %s\n", *argv);
// return 1;
}
else {
filecopy(fp, stdout);
fclose(fp);
}
}
}
if (ferror(stdout)) {
fprintf(stderr, "%s: error writing stdout\n", prog);
exit(2);
}
exit(0);
}
cat
最新推荐文章于 2025-03-20 21:10:34 发布