我试图将文件test1.mal
的内容复制到output.txt
中,程序说它正在这样做并且编译了所有内容,但是当我打开output.txt
文件时,它是空白...有人能告诉我我要去哪里吗?在C程序中复制文件,但文件为空
#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);' – chux
那么程序写入output.txt的地方在哪里呢?如果程序没有写入任何内容,则不会写入任何内容。 – immibis