题目:复制文件内容到新文件
代码:
#include <stdio.h>
#include <stdlib.h>
void file1(){
FILE *fp1,*fp2;
char ch,filename1[10],filename2[10];
printf("请输入读入的文件名:");
scanf("%s",filename1);
getchar();
printf("请输入写入的文件名:");
scanf("%s",filename2);
getchar();
if((fp1 = fopen(filename1,"r"))==NULL){
perror("open file1 failed:");
exit(0);
}
if((fp2 = fopen(filename2,"w"))==NULL){
perror("open file2 failed:");
exit(0);
}
ch = fgetc(fp1);
while(!feof(fp1)){
fputc(ch,fp2);
putchar(ch);
ch = fgetc(fp1);
}
putchar(10);
fclose(fp1);
fclose(fp2);
}
int main(){
file1();
return 0;
}
结果:
请输入读入的文件名:1.txt
请输入写入的文件名:2.txt
全心全意,为人民服务
坚持一个中国原则