晚上好,人们。我正在尝试将字符串打印到结果文件中,但是进展并不顺利。
例如,我的数据文件的内容是:

Hello, today
is a
Good Day
What is going
On here?


结果文件显示为:

On here?

g


这是我的打印功能:

void print(char B[])
{
    printf("%s", B); //NOTE; this string represents a line of data file and works!
    FILE *wfile;
    if ((wfile = fopen("result.txt","w")) == NULL){
       printf("File did not open!!!/n");
       exit(1);
   }
   fprintf(wfile, "%s", B);
}


我不知所措,非常感谢任何输入。

最佳答案

多亏了评论者,我才意识到

if ((wfile = fopen("result.txt","w")) == NULL)


我必须将“ w”更改为“ a”,因为我想将字符串附加到文件中

if ((wfile = fopen("result.txt","a")) == NULL)

10-08 16:03