fprintf函数不工作,但它返回正数 [英] fprintf function not working but it returns positive number

查看:335
本文介绍了fprintf函数不工作,但它返回正数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方式使用 fprintf 。一切似乎都确定,但 fprintf 根本不打印到我的文件!

  fprintf(pFile,%s\\\
,print);

奇怪的是 fprintf OK 。它会在上面的代码中返回 6 ,但不会打印到文件!



文件已成功创建, 。



将其更改为 printf 正在打印, OK c> fprintf 并且其他stdio输出函数都是缓冲的,这意味着输出首先存储在存储器中,并且直到稍后才实际打印。当使用标准输出打印到屏幕时,每个新行刷新缓冲区,因此使用 printf 可以立即看到输出,但是当打印到文件时,缓冲区不会刷新,直到写入(例如)4096个字节。您可以添加 fflush(pFile); 刷新缓冲区,如果由于某种原因需要输出快速出现在文件上。



当调用 fclose 时,缓冲区也被刷新,或者通过正确退出程序隐式关闭文件,但如果程序在不关闭文件的情况下继续运行,或者如果崩溃,则需要 fflush 才能查看文件的输出。


I'm using fprintf in the following way. Everything seems to be ok but fprintf doesn't print to my file at all!

fprintf(pFile, "%s\n", "print");

Something that is strange is that fprintf returns OK. it returns 6 in the above code, but not printing to file!

The file is created successfully but is empty.

changing it to printf is printing and OK too.

解决方案

fprintf and the other stdio output functions are buffered, which means that the output is first stored in memory, and not actually printed until later. When printing to the screen using standard output, each new line flushes the buffer, so with printf you would see the output immediately, but when printing to a file the buffer won't be flushed until you have written (for example) 4096 bytes. You can add fflush(pFile); to flush the buffer, if you for some reason need the output to appear on the file quickly.

The buffer is also flushed when calling fclose, or closing the file implicitly by properly exiting the program, but if the program keeps running without closing the file, or if it crashes, you will need fflush to see the output on the file.

这篇关于fprintf函数不工作,但它返回正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆