试程序出现很多“屯屯屯屯屯”
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 30 void copy(FILE * fp1,FILE * fp2); int main(void) { char rce_file[MAX],tar_file[MAX]; // 储存源文件和目标文件的文件名 FILE * fp1,* fp2; // 两个文件指针 char ch; printf("input name of source file: "); gets(rce_file); strcat(rce_file,".txt"); // 打开的是文本文档 printf("input name of target file: "); gets(tar_file); strcat(tar_file,".txt"); if((fp1 = fopen(rce_file,"rb")) == NULL) // 只读二进制模式 { printf("can't open %s file",rce_file); exit(1); } if((fp2 = fopen(tar_file,"a+b")) == NULL) // 读写二进制模式 { printf("can't open %s file",tar_file); exit(2); } copy(fp1,fp2); // 调试复制函数 while((ch = getc(fp2)) != EOF) // 查看目标文件被复制的内容 (如果这段代码不写就不会出现“屯屯屯屯屯”) putc(ch,stdout); fclose(fp1); // 关闭文件 fclose(fp2); system("pause"); return 0; } void copy(FILE * fp1,FILE * fp2) { char copy_s; while(EOF != (copy_s = fgetc(fp1))) fputc(copy_s,fp2); }