zoukankan      html  css  js  c++  java
  • (转,精彩!清空缓存的方法)C/C++ 误区二:fflush(stdin)

    2008-5-7 12:02:00
    1.       为什么 fflush(stdin) 是错的

    首先请看以下程序:

    #include <stdio.h>

    int main( void )

    {

        int i;

        for (;;) {

            fputs("Please input an integer: ", stdout);

            scanf("%d", &i);

            printf("%d\n", i);

        }

        return 0;

    }

    这个程序首先会提示用户输入一个整数,然后等待用户输入,如果用户输入的是整数,程序会输出刚才输入的整数,并且再次提示用户输入一个整数,然后等待用户输入。但是一旦用户输入的不是整数(如小数或者字母),假设 scanf 函数最后一次得到的整数是 2 ,那么程序会不停地输出“Please input an integer: 2”。这是因为 scanf("%d", &i); 只能接受整数,如果用户输入了字母,则这个字母会遗留在“输入缓冲区”中。因为缓冲中有数据,故而 scanf 函数不会等待用户输入,直接就去缓冲中读取,可是缓冲中的却是字母,这个字母再次被遗留在缓冲中,如此反复,从而导致不停地输出“Please input an integer: 2”。

    也许有人会说:“居然这样,那么在 scanf 函数后面加上‘fflush(stdin);,把输入缓冲清空掉不就行了?”然而这是错的!CC++标准里从来没有定义过 fflush(stdin)。也许有人会说:“可是我用 fflush(stdin) 解决了这个问题,你怎么能说是错的呢?”的确,某些编译器(如VC6)支持用 fflush(stdin) 来清空输入缓冲,但是并非所有编译器都要支持这个功能(linux 下的 gcc 不支持),因为标准中根本没有定义 fflush(stdin)MSDN 文档里也清楚地写着fflush on input stream is an extension to the C standardfflush 操作输入流是对 C 标准的扩充)。当然,如果你毫不在乎程序的移植性,用 fflush(stdin) 也没什么大问题。以下是 C99 fflush 函数的定义:

    int fflush(FILE *stream);

     

    如果 stream 指向输出流或者更新流update stream),并且这个更新流
    最近执行的操作不是输入,那么 fflush 函数将把这个流中任何待写数据传送至
    宿主环境(host environment)写入文件。否则,它的行为是未定义的。

    原文如下:


    int fflush(FILE *stream);

    If stream points to an output stream or an update stream in which
    the most recent
    operation was not input, the fflush function causes
    any unwritten data for that
    stream to be delivered to the host environment
    to be written to the file;
    otherwise, the behavior is undefined.

     

    其中,宿主环境可以理解为操作系统或内核等。

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

     

     

    1. 2.       清空输入缓冲区的方法

            虽然不可以用 fflush(stdin),但是我们可以自己写代码来清空输入缓冲区。只需要在 scanf 函数后面加上几句简单的代码就可以了。

            /* C 版本 */
            #include <stdio.h> 


            int main( void )
            {
                int i, c;
                
    for ( ; ; )
                {
                    fputs("Please input an integer: ", stdout);
                    scanf("%d", &i);

                    if ( feof(stdin) || ferror(stdin) )
                    {
    /* 如果用户输入文件结束标志(或文件已被读完), */
                      /* 或者发生读写错误,则退出循环               */

               
                        /* do something */
                        break;
                    }
                   
    /* 没有发生错误,清空输入流。                 */
                    /* 通过 while 循环把输入流中的余留数据“吃”掉 */
                    while ( (c = getchar()) != '\n' && c != EOF ) ;
                    /* 使用 scanf("%*[^\n]");(详见下面备注①) 也可以清空输入流, */

                    /* 不过会残留 \n 字符。                          */

                   printf("%d\n", i);
                }

                 return 0;
            }


           
    /* C++ 版本 */
            #include <iostream
    >
            #include <limits> // 为了使用numeric_limits

     

            using std::cout;
            using std::endl;
            using std::cin;
            using std::numeric_limits;
            using std::streamsize;

     

            int main()
            {
                int value;
                for ( ; ; )
                {
                    cout << "Enter an integer: ";
                    cin >> value;
                    if ( cin.eof() || cin.bad() )
                    {
    // 如果用户输入文件结束标志(或文件已被读完),
                      // 或者发生读写错误,则退出循环

                      // do something
                        break;
                    }
                   
    // 读到非法字符后,输入流将处于出错状态
                    // 为了继续获取输入,首先要调用 clear 函数
                    // 来清除输入流的错误标记,然后才能调用
                    // ignore 函数来清除输入流中的数据。
                    cin.clear();
                    
    // numeric_limits<streamsize>::max() 返回输入缓冲的大小。
                    // ignore 函数在此将把输入流中的数据清空。
                    // 这两个函数的具体用法请读者自行查询。

                    cin.ignore( numeric_limits<streamsize>::max(), '\n' );

                    cout << value << '\n';
                }

                return 0;
            }

    参考资料

    ISO/IEC 9899:1999 (E) Programming languages C 7.19.5.2 The fflush function

    The C Programming Language 2nd Edition By Kernighan & Ritchie

    ISO/IEC 14882(1998-9-01)Programming languages C++

    ****************************************
    备注①:

       星号(*)表示读指定类型的数据但不保存
     
       [ ]  扫描字符集合——正则表达示,字符簇:当在一组方括号里使用^时,它表示“非”或“排除”的意思。
       [^\n] 定义一个集合, 即除'\n'外的任意字符

       详注: ANSI C 标准向 scanf() 增加了一种新特性,称为扫描集(scanset)。 扫描集定义一个字符集合,可由 scanf() 读入其中允许的字符并赋给对应字符数组。 扫描集合由一对方括号中的一串字符定义,左方括号前必须缀以百分号。 例如,以下的扫描集使 scanf() 读入字符 A、B 和 C:
        %[ABC]
       使用扫描集时,scanf() 连续吃进集合中的字符并放入对应的字符数组,直到发现不在集合中的字符为止(即扫描集仅读匹配的字符,一旦发现不匹配,马上返回)。返回时,数组中放置以 null 结尾、由读入字符组成的字符串。
        char nums[20]="";
        scanf("%[0-9]",nums);
        用字符 ^ 可以说明补集。把 ^ 字符放为扫描集的第一字符时,构成其它字符组成的命令的补集合,指示 scanf() 只接受未说明的其它字符。
        其他的详见《正则表达式》。


     
     



    一个例子
    xiaou发表评论于2007-7-16 14:09:40
    #include <iostream>
    #include <stdexcept>//标准库异常类
    #include <limits> // 为了使用numeric_limits
    using namespace std;
    int main()
    {
        int a,b;
        while(1){
            cin>>a>>b;
            try{
                if(cin.good()) cout<<a+b<<endl;
                else throw runtime_error("输入有误,重新输入?y OR n :");
            }
            catch(runtime_error err){
                cout<<err.what();
                char ch;
                cin.clear();
                cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                cin>>ch;
                if(cin.bad()||ch!='y') break;
            }
        }//whlie()
        system("pause");
    }

     
     
    另一个例子:C和C++杂合的清空缓存方法(荐)
    小鼬(游客)发表评论于2008-5-7 12:20:26

    #include<iostream>
    using namespace std;

    template <class T>
    class input{
          T t;
    public:
          input(char *s,T min,T max){
              cout<<s<<endl;
              while(1){
                  if(cin>>t)
                      if(t<=max && t>=min)
                          break;
                  //C和C++杂合的:
                  cin.clear();
          int c;
           while( (c=getchar())!='\n'&&c!=EOF );

              }
          }
    };

    int main()
    {
        input<int> o("test",-1,10);
        system("pause");
    }

    C++的更好的方法:
    2009-5-19 22:16:34
    1、使用cin.clear()清除错误状态
    2、用cin.sync() 清空输入缓存区
  • 相关阅读:
    qt.qpa.plugin Could not load the Qt platform plugin xcb
    深度学习模型在移动端的部署
    ccproxy
    Java版本
    第四篇 -- Go语言string转其他类型
    第三篇 -- Go语言其他类型转String方法
    第二篇 -- Go语言转义字符与变量声明
    第五十三篇 -- MFC美化界面2
    C++第五十二篇 -- 多线程之消息传递
    第一篇 -- Go环境的搭建以及Hello World程序编写
  • 原文地址:https://www.cnblogs.com/xiaouisme/p/2580478.html
Copyright © 2011-2022 走看看