NAME
  fflush - flush a stream

SYNOPSIS
  #include <stdio.h>

  int fflush(FILE *stream);

DESCRIPTION
  For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function.

      For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected.

  如果给出的文件流是一个输出流,那么fflush()把输出到缓冲区的内容写入文件. 如果给出的文件流是输入类型的,那么fflush()会清除输入缓冲区。

  fflush()在调试时很实用,特别是对于在程序中输出到屏幕前发生错误片段时. 直接调用 fflush( STDOUT )输出可以保证你的调试输出可以在正确的时间输出. 

  If the stream argument is NULL, fflush() flushes all open output streams.

For a nonlocking counterpart, see unlocked_stdio(3).

1.

flush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃 
fflush(stdout)刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上。

2.

fflush的真正作用就是立即将缓冲区中的内容输出到设备。正因为这样,所以只能在写入文件的时候使用fflush。在读取文件时使用fflush是不好的编程方法,因为那样的代码在一些环境下可能正常工作,但在另一些环境下则会出错,这导致程序不可移植。

 

flush即清空缓冲,在慢速设备上使用缓存是一种提升数据处理效率的手段,flush的作用是将内存中缓冲的内容实际写入外存媒介
详见MSDN的Kernel32!FlushFileBuffers

fclose后未必会flush一次的,操作系统会在CPU空闲的时候执行flush

 

3.

fflush不应该在读取文件的时候用,而应该在写入文件的时候用。
fflush会清空缓冲区,fclose在关闭文件之前也会清空缓冲区。如果使用exit函数退出程序,或者main函数返回,则所有打开后没有关闭的文件会自动关闭,关闭时也会清空缓冲区。

通常,只有在程序非正常结束的情况下,缓冲区才不会被清除。


参考

fflush 函数    http://blog.chinaunix.net/uid-21651805-id-3482290.html

fflush函数有什么作用?     http://blog.csdn.net/stpeace/article/details/9063293

转载自:http://www.cnblogs.com/wujing-hubei/p/5743509.html