Linux环境下清空标准输入缓冲区 fflush(stdin) 无效

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TMT123421/article/details/84565925

一、误区

不论从 字面意思 还是 某些技术文档 中,都可以看到 fflush(stdin) 是对标准输入缓冲区清空的标准方案;

然而,并不是!

深入了解一下 fflush(stdin) 之后,才发现这个锅得自己背,怪自己喜欢拿来就用,不去深入了解某些重要细节,给自己上了生动一课;

二、问题描述

环境:Ubuntu18.04

编译器:gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04)

描述:在 scanf() 循环获取用户输入的时候,首次输入数据,总是会执行循环两次(第一次获取到垃圾数据,第二次获取到刚才输入的数据),此刻已经意识到是标准输入缓冲区的问题,然后疯狂加 fflush(stdin) 之后,并没有效果(尴尬.jpg);试图查看逻辑,查看数据类型,发现并没有问题;最后自己搜索了一通 fflush() 函数,找到了问题所在(让我高兴一会儿);

三、解决方法

Linux环境下,可以使用下面两种方案清空标准输入的缓冲区:

// 方案一
void empty_stdin() {
    int c;
    do {
        c = getchar();
    } while (c != '\n' && c != EOF);
}

// 方案二
setbuf(stdin, NULL);

四、原因

fflush(stdin) 并非系统提供的标准定义,只是对系统函数的扩展而已,并没有得到编译器的统一支持;

所以,需要针对具体情况具体分析;

如下,摘自 Linux man手册 和 百度百科 对 fflush() 的描述:

# MAN文档中的部分描述
ubuntu@ubuntu:~/work/c/algo$ man 3 fflush

# NAME
       fflush - flush a stream

# SYNOPSIS
       #include <stdio.h>

       int fflush(FILE *stream);

# DESCRIPTION
       For  output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the
       stream's underlying write function.

       For input streams associated with seekable files (e.g., disk files, but not pipes or  terminals),  fflush()  discards  any
       buffered data that has been fetched from the underlying file, but has not been consumed by the application.

       The open status of the stream is unaffected.

       If the stream argument is NULL, fflush() flushes all open output streams.

       For a nonlocking counterpart, see unlocked_stdio(3).

# RETURN VALUE
       Upon successful completion 0 is returned.  Otherwise, EOF is returned and errno is set to indicate the error.

# 百度百科中对 fflush 使用的注意事项

C 和 C++ 的标准里从来没有定义过 fflush(stdin)。也许有人会说:“可是我用 flush(stdin) 解决了这个问题,你怎么能说是错的呢?”的确,某些编译器(如VC6)支持用 fflush(stdin) 来清空输入缓冲,但是并非所有编译器都要支持这个功能(linux下的gcc就不支持,经我的GCC4.6.2测试),因为标准中根本没有定义fflush(stdin)。

MSDN 文档里也清楚地写着:

fflush on input stream is an extension to the C standard (fflush 操作输入流是对C标准的扩充)。
以下是 C99 对 fflush 函数的定义:

int fflush(FILE *stream);

如果stream指向输出流或者更新流(update stream),并且这个更新流最近执行的操作不是输入,那么fflush函数将把任何未被写入的数据写入stream指向的文件(如标准输出文件stdout)。否则,fflush函数的行为是不确定的。

fflush(NULL)清空所有输出流和上面提到的更新流。如果发生写错误,flush函数会给那些流打上错误标记,并且返回EOF,否则返回0。

由此可知,如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的。故而使用 fflush(stdin) 是不正确的。

—— 2018-11-27 ——

猜你喜欢

转载自blog.csdn.net/TMT123421/article/details/84565925
今日推荐