这里我尝试使用此代码将file1
的内容复制到file2
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fs,*ft;
int ch;
fs=fopen("C:\\Users\\BRAHMA JAISWAL\\Desktop\\a.txt","a+");
if(fs==NULL){
puts("Unable to open Source file");
exit(1);
}
ft=fopen("C:\\Users\\BRAHMA JAISWAL\\Desktop\\b.txt","wb");
if(ft==NULL)
{
puts("Unable to open TArget File");
fclose(ft);
exit(1);
}
puts("Enter anything which you want to write in source file:");
int a;
scanf("%d",&a);
fprintf(fs,"%d",a);
while(1)
{
ch=fgetc(fs);
if(ch==EOF)break;
fputc(ch,ft);
}
fclose(ft);
fclose(fs);
return 0;
}
它不起作用(file1的内容没有复制到file2)但是当我删除这部分代码时它就可以正常工作。
puts("Enter anything which you want to write in a source file:");
int a;
scanf("%d",&a);
fprintf(fs,"%d",a);
谁能说出问题所在吗?
最佳答案
您的fprintf(fs,"%d",a);
实际上打印到您的源文件,而不是您的目标文件。
源文件可能应该以 r
模式打开,而不是 a+
。
关于c - while 循环在文件复制中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49210095/