#include <stdio.h> /* FILE *fp; 【1】FILE *fopen(char *name, char *mode); 【2】// 流fp出現(xiàn)錯誤,返回非0值 int ferror(FILE *fp) 【3】// 到達(dá)文件末尾,返回非0值 int feof(FILE *fp) 【4】// 返回fp指向的輸入流中的下一個字符,文件尾或錯誤返回EOF int getc(FILE *fp); 【5】// 將字符c寫入到fp指向的文件中,并返回寫入的字符。錯誤返回EOF int putc(int c, FILE *fp); 【6】 #define getchar() getc(stdin) #define putchar() putc((c), stdout) 【7】 int fscanf(FILE *fp, char *format, ...) int fprintf(FILE *fp, char *format, ...) 【8】 // 從fp中讀取maxline - 1 個字符并以'\0'結(jié)尾保存到數(shù)組line中。 // 通常返回line遇文件結(jié)尾或發(fā)生錯誤返回NULL。 char *fgets(char *line, int maxline, FILE *fp) // 將一個字符串寫入到一個文件中。 // 發(fā)生錯誤返回EOF,否則返回一個非負(fù)值。 char *fputs(char *line, FILE *fp) 【9】 // 與fgets和fputs類似,但其對stdin,stdout進(jìn)行操作 gets在每行末尾刪除'\n' puts在每行末尾添加'\n' char *gets(char *s); int puts(const char *s); */ /* cat函數(shù)的實現(xiàn) */ int main(int argc, char *argv[]) { FILE *fp; void filecopy(FILE *, FILE *); if(argc == 1) /* 若沒有命令行參數(shù),則復(fù)制標(biāo)準(zhǔn)輸入 */ filecopy(stdin,stdout); else while(--argc > 0) if((fp = fopen(*++argv, "r")) == NULL) { printf("cat:can't open %s\n", *argv); return 1; } else { filecopy(fp, stdout); fclose(fp); } return 0; } /* filecopy函數(shù): 將文件ifp復(fù)制到文件ofp */ void filecopy(FILE *ifp, FILE *ofp) { int c; while((c = getc(ifp)) != EOF) putc(c, ofp); } |
|