fflush 函数文档
函数概要:
fflush 函数用于将缓冲区内的数据强制写入指定的文件中。
函数原型:
#include <stdio.h> ... int fflush(FILE *stream);
参数解析:
|
参数 |
含义 |
|
stream |
1. 该参数是一个 FILE 对象的指针,指定待更新的文件对象 |
返回值:
1. 如果函数调用成功,返回值是 0;
2. 如果函数调用失败,返回 EOF 并设置 errno 为指定的错误。
演示:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buff[1024];
memset(buff, '\0', sizeof(buff));
// 指定 buff 为缓冲区,_IOFBF 表示当缓冲区已满时才写入 stdout
setvbuf(stdout, buff, _IOFBF, 1024);
fprintf(stdout, "This is bbs.fishc.com\n");
fprintf(stdout, "This output will go into buff\n");
// fflush强制将上面缓存中的内容写入stdout
fflush(stdout);
fprintf(stdout, "this will appear when progream\n");
fprintf(stdout, "will come after sleeping 5 seconds\n");
sleep(5);
return 0;
}




