C fprintf()和fputs()未获得正确的结果
大家晚上好。我正在尝试将字符串打印到结果文件中,但是事情进展得不太顺利。 例如,我的数据文件的内容包括:C fprintf()和fputs()未获得正确的结果,c,string,file,printf,output,C,String,File,Printf,Output,大家晚上好。我正在尝试将字符串打印到结果文件中,但是事情进展得不太顺利。 例如,我的数据文件的内容包括: 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
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)
您的
fopen
模式为“w”(write),您可能希望将其更改为append。此外,您应该在编写文件后,<代码> fCuts文件。您可能还需要考虑在程序开始时打开文件并将文件*传递到您的打印功能,这样您就可以多次打印而不重复打开和关闭文件,我爱您的人!!最后,它成功了,当然这是世界上最简单的修复方法。