c里面的fflush函数

NAME
  fflush - flush a streamhtml

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.app

      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.ide

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

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

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

For a nonlocking counterpart, see unlocked_stdio(3).spa

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

相关文章
相关标签/搜索