fflush函数
概要:
#include <stdio.h>
int fflush(FILE *stream);
描述:
如果参数stream指向的流是输出流或者最近操作不是输入操作的更新流,该函数将流的任何未写入数据传递到宿主环境(host enviroment)以便写入文件;否则函数行为是未定义的。
如果参数stream是空指针,fflush函数会对所有先前定义过行为的流执行刷新操作。
参数:
FILE *stream
指向要刷新的流的指针。
返回值:
如果发生写入错误(write error),函数为流设置错误指示符,并返回EOF;否则函数返回0。
范例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
/*函数fflush范例*/
#include <stdio.h>
#include <time.h>
#define STEP 5
/*设置等待时间的函数。*/
void my_sleep(unsigned int second)
{
time_t interval = time(NULL) + second;
/*等待中。*/
while(time(NULL)<interval)
{
;
}
}
int main(void)
{
/*开始下载。*/
puts("Start downloading:");
/*输出下载进度条。*/
for(int i=0; i<=100; i+=STEP)
{
/*回到行首。*/
printf("\r");
int block = i/STEP;
for(int j=0; j<100/STEP; j++)
{
if(j<block)
{
printf("\u25A0");
}
else
{
printf("\u25A1");
}
}
printf(" %3d%%", i);
fflush(stdout);
if(i<100)
my_sleep(1);
}
/*下载完成。*/
puts("\nDownload completed!");
return 0;
}
|
输出:
Start downloading:
■■■■■■■■■■■■■■■■■■■■ 100%
Download completed!
相关内容:
主要参考资料:
1、ISO/IEC 9899:2024
2、ISO/IEC 9899:2018