当前位置:   article > 正文

linux系统fflush函数_linux fflush

linux fflush

在linux系统里面,一般都是行刷新,也就是要输出的内容会先放在缓冲区里面,直到遇到换行符,才会将缓冲区里的内容全部输出到屏幕或者文件中。

函数原型

  1. #include <stdio.h>
  2. int fflush(FILE *stream);

参数
        FILE *stream — 文件指针

作用
用来清空文件缓冲区
两个特殊参数
        stdout : standard output 的缩写,即标准输出,一般是指显示器;标准输出缓冲区即是用来暂存将要显示的内容的缓冲区。
        fflush(stdout); 作用: 清空标准输出缓冲区,并将缓冲区的内容通过显示器打印出来。
        stdin: standard input 的缩写,即标准输入,一般是指键盘;标准输入缓冲区即是用来暂存从键盘输入的内容的缓冲区。
        fflush(stdin); 作用:清空标准输入缓冲区。

代码示例1:未使用fflush函数的例子

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. int main()
  4. {
  5. char buf[1024] = {0};
  6. printf("Please Enter# ");
  7. // fflush(stdout);
  8. int s = read(0,buf,sizeof(buf));
  9. if(s > 0)
  10. {
  11. printf("output: %s\n",buf);
  12. }
  13. return 0;
  14. }

运行结果如下:

 代码示例2:使用fflush函数的例子

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. int main()
  4. {
  5. char buf[1024] = {0};
  6. printf("Please Enter# ");
  7. fflush(stdout);
  8. int s = read(0,buf,sizeof(buf));
  9. if(s > 0)
  10. {
  11. printf("output: %s\n",buf);
  12. }
  13. return 0;
  14. }

运行结果如下:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/334877
推荐阅读
相关标签
  

闽ICP备14008679号