1 //用fgetc()/fputc()实现文件的拷贝 2 #include3 #define F_PRINT_ERR(e)\ 4 do\ 5 {\ 6 if(e == NULL)\ 7 {\ 8 printf("open error");\ 9 exit(-1);\10 }\11 }\12 while(0)13 int main(int argc,char* argv[])14 {15 16 //基本实现:17 FILE* pfr = fopen("G:/qtcode/pfr.txt","r");18 F_PRINT_ERR(pfr);19 20 FILE* pfw = fopen("G:/qtcode/pfw.txt","w+");21 F_PRINT_ERR(pfw);22 char ch;23 while((ch = fgetc(pfr)) != EOF)24 {25 putchar(fputc(ch,pfw));//顺便打印在屏幕上26 }27 fclose(pfr);28 fclose(pfw);29 30 #if 031 //实现CP命令32 FILE* pfr = fopen(argv[1],"r");33 F_PRINT_ERR(pfr);34 35 FILE* pfw = fopen(argv[2],"w+");36 F_PRINT_ERR(pfr);37 while((ch = fgetc(pfr)) != EOF)38 {39 putchar(fputc(ch,pfw));//顺便打印在屏幕上40 }41 fclose(pfr);42 fclose(pfw);43 #endif44 45 return 0;46 }