我在将文件的文本复制到另一个新文件时遇到问题。它打开,创建一个新文件,但上面没有任何内容。它没有复制第一个文件的文本。目前,这是代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char content[80];
char newcontent[80];
//Step 1: Open text files and check that they open//
FILE *fp1, *fp2;
fp1 = fopen("details.txt","r");
fp2 = fopen("copydetails.txt","w");
if(fp1 == NULL || fp2 == NULL)
{
printf("Error reading file\n");
exit(0);
}
printf("Files open correctly\n");
//Step 2: Get text from original file//
while(fgets(content, sizeof(content), fp1) !=NULL)
{
fputs (content, stdout);
strcpy (content, newcontent);
}
printf("%s", newcontent);
printf("Text retrieved from original file\n");
//Step 3: Copy text to new file//
while(fgets(content, sizeof(content), fp1) !=NULL)
{
fprintf(fp2, newcontent);
}
printf("file created and text copied to it");
//Step 4: Close both files and end program//
fclose(fp1);
fclose(fp2);
getch();
return 0;
}
最佳答案
while(fgets(content, sizeof(content), fp1) !=NULL){
fprintf(fp2, "%s", content);
}
关于c - 使用C编程语言将文本文件的内容复制到另一个文本文件中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22836437/