【发布时间】:2017-09-02 04:23:45
【问题描述】:
我正在尝试将文件 test1.mal 的内容复制到 output.txt 中,并且程序说它正在这样做并且所有内容都可以编译,但是当我打开 output.txt 文件时,它是空白的...谁能告诉我哪里出错了?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char content[255];
char newcontent[255];
FILE *fp1, *fp2;
fp1 = fopen("test1.mal", "r");
fp2 = fopen("output.txt", "w");
if(fp1 == NULL || fp2 == NULL)
{
printf("error reading file\n");
exit(0);
}
printf("files opened correctly\n");
while(fgets(content, sizeof (content), fp1) !=NULL)
{
fputs(content, stdout);
strcpy (content, newcontent);
}
printf("%s", newcontent);
printf("text received\n");
while(fgets(content, sizeof(content), fp1) !=NULL)
{
fprintf(fp2, "output.txt");
}
printf("file created and text copied\n");
//fclose(fp1);
//fclose(fp2);
//return 0;
}
【问题讨论】:
-
strcpy (content, newcontent);的意义何在?调试?newcontent未初始化!也许你想要strcpy (newcontent, content); -
那么程序写入 output.txt 的部分在哪里?如果程序不写任何东西,那么什么都不会写。
标签: c file-io file-copying