赞
踩
在linux系统里面,一般都是行刷新,也就是要输出的内容会先放在缓冲区里面,直到遇到换行符,才会将缓冲区里的内容全部输出到屏幕或者文件中。
函数原型
- #include <stdio.h>
- int fflush(FILE *stream);
参数
FILE *stream — 文件指针
作用
用来清空文件缓冲区
两个特殊参数
stdout : standard output 的缩写,即标准输出,一般是指显示器;标准输出缓冲区即是用来暂存将要显示的内容的缓冲区。
fflush(stdout); 作用: 清空标准输出缓冲区,并将缓冲区的内容通过显示器打印出来。
stdin: standard input 的缩写,即标准输入,一般是指键盘;标准输入缓冲区即是用来暂存从键盘输入的内容的缓冲区。
fflush(stdin); 作用:清空标准输入缓冲区。
代码示例1:未使用fflush函数的例子
- #include <stdio.h>
- #include <unistd.h>
-
- int main()
- {
- char buf[1024] = {0};
- printf("Please Enter# ");
- // fflush(stdout);
- int s = read(0,buf,sizeof(buf));
- if(s > 0)
- {
- printf("output: %s\n",buf);
- }
- return 0;
- }
运行结果如下:
代码示例2:使用fflush函数的例子
- #include <stdio.h>
- #include <unistd.h>
-
- int main()
- {
- char buf[1024] = {0};
- printf("Please Enter# ");
- fflush(stdout);
- int s = read(0,buf,sizeof(buf));
- if(s > 0)
- {
- printf("output: %s\n",buf);
- }
- return 0;
- }
-

运行结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。