| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1454 人关注过本帖
标题:关于fflush
只看楼主 加入收藏
lusan168
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-11-17
收藏
 问题点数:0 回复次数:4 
关于fflush
请问fflush(stdin)一般在什么情况才用
请各位高手总结点经验出来吧
我个人认为在接收字符串的时候;
但是重复接收几个数组的字符串时,到底是每条语句接收前都要加fflush(stdin);还是只在第一天语句加呢>

好象都难以说清楚,有时候不会错,有时候也会错.
搜索更多相关主题的帖子: fflush 
2007-11-24 00:48
neverTheSame
Rank: 3Rank: 3
来 自:江西农业大学
等 级:新手上路
威 望:9
帖 子:1511
专家分:0
注 册:2006-11-24
收藏
得分:0 
fflush(stdin);是用来清除标准输入准备的缓冲区的.
当缓冲区中有内容的时侯,而你又不想要获取这样的内容,
你就可以用它了.
如下例子:
char ch,ch1;
scanf("%c",&ch); /*由于按回车换行键的时侯会
将其分成回车符和换行符,换行符留在缓冲区中.如果不用fflush(stdin);
下一个接受的字符,应该当是换行符(ASCII值为10)*/
fflush(stdin);
scanf("%c",&ch1);

wap酷禾网(http://wap.),提供免费的、优质的、快捷的wap资源下载服务。
2007-11-24 00:57
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
一般来说,就是在每个scanf前加一句,比较保险

从BFS(Breadth First Study)到DFS(Depth First Study)
2007-11-24 08:29
VxWorks
Rank: 3Rank: 3
来 自:WindRiver
等 级:论坛游民
威 望:6
帖 子:859
专家分:68
注 册:2007-11-24
收藏
得分:0 
2楼的论坛元老果然够强!:L
基础不扎实混论坛学的都是些花拳绣腿。
====================================================================
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);’,把输入缓冲清空掉不就行了?”然而这是错的!C和C++的标准里从来没有定义过 fflush(stdin)。也许有人会说:“可是我用 fflush(stdin) 解决了这个问题,你怎么能说是错的呢?”的确,某些编译器(如VC6)支持用 fflush(stdin) 来清空输入缓冲,但是并非所有编译器都要支持这个功能(linux 下的 gcc 就不支持),因为标准中根本没有定义 fflush(stdin)。MSDN 文档里也清楚地写着fflush on input stream is an extension to the C standard(fflush 操作输入流是对 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)  是不正确的,至少是移植性不好的。

 

 

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++
2007-11-24 11:14
YoungZ1
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-9-20
收藏
得分:0 
2L & 4L 厉害呀,膜拜!!
2015-07-22 10:46
快速回复:关于fflush
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022018 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved