参考自linux下如何清空(刷新)stdin缓冲区

首先,fflush在C/C++/POSIX标准中只定义了处理输出流的行为,对于像stdin这种输入流,这是未定义行为undefined behavior,随便C/C++库怎么去实现都不算错。即使某个C/C++库对fflush(stdin)的处理是删除掉你硬盘上所有文件都没有错。所以你根本不要指望任何未定义行为能在不同平台下有相同的表现。



再来看看linux对fflush(stdin)的说法

代码:
man 3 fflush
引用:
For input streams, fflush() discards any buffered data that has been

fetched from the underlying file, but has not been consumed by the application.

说的是如果对fflush传入一个输入流,会清除已经从输入流中取出但还没有交给程序的数据



注意红字部分。fflush处理的是已经从输入流中取出的数据,而不是输入流中剩余的数据。而且这数据还不能交给程序,也就是说,对于getchar这种函数,只有其还未返回时,stdin的输入缓冲区才可能有数据,此时调用fflush才会有用

代码:
man 3 stdin
引用:
Note that in case stdin is

associated with a terminal, there may also be input buffering in the

terminal driver, entirely unrelated to stdio buffering. (Indeed, nor‐

mally terminal input is line buffered in the kernel.)

和上面连起来看就更明白了,终端驱动中接收你输入文本的缓冲区就相当于是输入流,和stdin的缓冲区是两回事



而在glibc的文档中,关于fflush的说明如下

http://www.gnu.org/software/libc/manual/html_node/Flushing-Buffers.html

引用:
This function causes any buffered output on stream to be delivered to the file. If stream is a null pointer, then fflush causes buffered output on all open output streams to be flushed.

连输入流都没提到。说明glibc实现中的fflush要么对输入流什么都没干,要么干了些对外部完全没有影响的事



要想实现你所希望的效果,可以用非标准的fpurge()。glibc中的近似实现是__fpurge()。自己去

代码:
man 3 fpurge

linux下fflush(stdin)的使用问题的更多相关文章

  1. 解决linux下fflush(stdin)无效

    void clean_stdin(void) { int c; do { c = getchar(); } while (c != '\n' && c != EOF); }

  2. linux下centos解压时报错: gzip: stdin: not in gzip format   tar: Child returned status 1   tar: Error is not recoverable: exiting now

    最近在linux下安装python时,解压Python.tgz文件时遇到一个问题:          gzip: stdin: not in gzip format      tar: Child r ...

  3. ACM的fflush(stdin)的问题

    在最近的刷题过程中,因为用到了很多字符串的操作,有时需要多次清空缓冲区,所以用了fflush(stdin);的语句,确实很好用,但是发现在OJ上提交后会出现runtime error的问题.当时并没有 ...

  4. (转)关于fflush(stdin)清空输入缓存流(C/C++)

    来源:http://my.oschina.net/deanzhao/blog/79790 1. 为什么 fflush(stdin) 是错的?首先请看以下程序: #include <stdio.h ...

  5. c++ 网络编程(七) LINUX下 socket编程 基于套接字的标准I/O函数使用 与 fopen,feof,fgets,fputs函数用法

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9614820.html 一.标准I/O 1,什么是标准I/O?其实是指C语言里的文件操作函数,如 ...

  6. fflush(stdin)的对错?

    C和C++的标准里从来没有定义过 fflush(stdin).也许有人会说:"可是我用 fflush(stdin) 解决了这个问题,你怎么能说是错的呢?"的确,某些编译器(如VC6 ...

  7. linux下 open fopen区别

    open是linux下的底层系统调用函数,fopen与freopen c/c++下的标准I/O库函数,带输入/输出缓冲.linxu下的fopen是open的封装函数,fopen最终还是要调用底层的系统 ...

  8. Linux下反弹shell的种种方式

    [前言:在乌云社区看到反弹shell的几种姿势,看过之余自己还收集了一些,动手试了下,仅供参考] 0x01 Bash bash -i >& /dev/tcp/ >& 这里s ...

  9. Linux下,拷贝文件时,排除某些文件

     一下是自己用到到几次实践,觉得很赞:   1.拷贝文件时,排除某些不需要的文件:   1)使用xargs来做: ls /tmp/test/ |grep -v .gz |xargs -i cp -r ...

随机推荐

  1. MongoDB基本操作

    转:http://zhidao.baidu.com/link?url=D5s4tNnP6hH0XPZkFooV-o4MQH3pNZh7C3rtLX_HtVWaIyBRhLIUyoZYfVv15l2eS ...

  2. C#学习笔记2:Iframe框架、MD5加密

    1.static void Main()的问题. static void Main(){……//代码}static void Main(string[] args){……//代码}两者的不同点?str ...

  3. linux运维常用命令

    1.linux启动过程 开启电源 --> BIOS开机自检 --> 引导程序lilo或grub--> 内核的引导(kernel boot)--> 执行init(rc.sysin ...

  4. .net 中连接mysql

    1. 下载mysql驱动.里面包含需要连接mysql的dll.mysql-connector-net    地址:http://dev.mysql.com/downloads/file/?id=463 ...

  5. Microsoft SQL Server Product Samples:Database

    从SQL Server 2005 之后示例数据都为AdventureWorks,需要的通过codeplex网站下载.这样设计的目的应该在于是生产库行不必要的用户以及权限分配. 从以下网址访问http: ...

  6. 浅析foreach原理

    在日常开发工作中,我们发现很多对象都能通过foreach来遍历,比如HashTable.Dictionary.数组等数据类型.那为何这些对象能通过foreach来遍历呢?如果写一个普通的Person类 ...

  7. xmlDoc.SelectNodes用法(获取不到节点时注意事项)

    注:以下举例仅针对xml自定义了命名空间的情况,如果是其他情况,请参照他人博客~ using System;using System.Collections.Generic;using System. ...

  8. Java实战之04JavaWeb-02Request和Response

    一.Response和Request的生态环境 二.HttpServletResponse---代表响应对象 1.设置状态码 void setStatus(int sc) 状态码: 200:一切正常 ...

  9. RabbitMQ RPC问题

    1.服务器端代码:https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/rpc_server.py 2.客户端代码:htt ...

  10. 用EPPlus导入导出数据到excel

    项目上中要用到将数据库中所有表导出为Excel,以及将Excel数据导入数据库中的操作,使用EPPlus组件,编写以下两个函数. using OfficeOpenXml;using OfficeOpe ...