c 语言函数 fflush()

转载自(http://www.cplusplus.com/reference/clibrary/cstdio/fflush/)

int fflush ( FILE * stream );

Flush stream

If the given  stream was open for writing and the last i/o operation was an output operation, any unwritten data in the output buffer is written to the file.
If it was open for reading and the last operation was an input operation, the behavior depends on the specific library implementation. In some implementations this causes the input buffer to be cleared, but this is not standard behavior.
If the argument is a null pointer, all open files are flushed.
The stream remains open after this call.
When a file is closed, either because of a call to  fclose or because the program terminates, all the buffers associated with it are automatically flushed.

Parameters

stream
Pointer to a  FILE object that specifies a buffered stream.

Return Value

A zero value indicates success.
If an error occurs,  EOF is returned and the error indicator is set (see  feof).

Example

In files open for update (i.e., open for both reading and writting), the stream shall be flushed after an output operation before performing an input operation. This can be done either by repositioning (fseekfsetposrewind) or by calling explicitly fflush, like in this example:

/* fflush example */
#include <stdio.h>
char mybuffer[80];
int main()
{
   FILE * pFile;
   pFile = fopen ("example.txt","r+");
   if (pFile == NULL) perror ("Error opening file");
   else {
     fputs ("test",pFile);
     fflush (pFile);    // flushing or repositioning required
     fgets (mybuffer,80,pFile);
     puts (mybuffer);
     fclose (pFile);
     return 0;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值