fflush

@求fflush(stdin)和fflush(stdout)用法

#include<stdio.h>
void main()
{
int a,b,c,d;
fflush(stdin);
scanf("%d%d%d",&a,&b,&c);
if(a > b)
  d = a;
else
  d = b;
if(c > d)
  d = c;
printf("%d/n",d);
fflush(stdout);
}
这样的一个问题。
假如输入的不是3个而是大于3个时候,是不是要清空缓冲区呢?
输出的时候是不是也要清空一下呢?
谢谢您帮我解答。
3Q
 
@【gfxiang】:
一般不用。
btw: fflush()似乎只对输出缓冲区清空,对输入缓冲区清空可以用
do { int c = getc(); } while ( c != EOF && c != '/n');


【iu_81】:
flush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃
fflush(stdout)刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上。

【iu_81】:
scanf()函数接收输入数据时,遇以下情况结束一个数据的输入:(不是结束该scanf函数,scanf函数仅在每一个数据域均有数据,并按回车后结束)。
 ① 遇空格、“回车”、“跳格”键。
 ② 遇宽度结束。
 ③ 遇非法输入。
键盘缓冲区就可能有残余信息问题。
scanf()函数应该只是扫描stdin流,这个残存信息是在stdin中
解决就要在scanf()函数之后加个fflush(stdin)

【iu_81】:
fflush(stdout)刷新stdout,因为printf是缓冲输出,一般情况下stdout是行缓冲。 
单进程环境中这个函数在这儿没什么用,在多进程/多线程时就有用了。

【comman_ndsc】:
#include<stdio.h>
void main()
{
int a,b,c,d;
fflush(stdin);    // 单在这个程序里面,这句话是不起任何作用的,fflush(stdin)是一个未定义行为,也就是在标准c里面并没有规定有这样的用法,但是有的编译器内定了这样也是合法的,比如vc,而在有的编译器下这就可能是一条错误的语句,比如gcc。所以这样写并不是一种好的习惯。你可以把这条语句换成:while(getchar() != '/n') ;
scanf("%d%d%d",&a,&b,&c);
if(a > b)
d = a;
else
d = b;
if(c > d)
d = c;
printf("%d/n",d);
fflush(stdout);
}
这样的一个问题。
假如输入的不是3个而是大于3个时候,是不是要清空缓冲区呢?// 如果楼主是想把多余的输入清除的话,那就应该在输入语句之后加一条 while(getchar() != '/n') ;这样可以保证,输入缓冲区中只有三个数。

输出的时候是不是也要清空一下呢?// 这个到是很少用
谢谢您帮我解答。


#include<stdio.h>
void main()
{
int a,b,c,d;
scanf("%d%d%d",&a,&b,&c);
while (getchar () != '/n')   // 吃掉多余的输入
{
;
}
if(a > b)
d = a;
else
d = b;
if(c > d)
d = c;
printf("%d/n",d);

}

@C语言fflush(stdin)函数是什么意思,在什么情况下用它
 悬赏分:0 - 解决时间:2006-8-25 21:31
我在有些C程序中见到fflush(stdin),不知道它是干什么用的,请问在什么情况下用它?另外,本人的联想能力也挺丰富的,本人想既然有fflush(stdin),那是否也有fflush(stdout)?如果有,它又是干什么用的?
提问者: sunkefei521 - 助理 二级
最佳答案
清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件

没有你后面说的那个。

例子:
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
void flush(FILE *stream);
int main(void)
{
FILE *stream;
char msg[] = "This is a test";
stream = fopen("DUMMY.FIL", "w");
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf("Press any key to flush DUMMY.FIL:");
getch();
flush(stream);
printf("
File was flushed, Press any key to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
fflush(stream);
duphandle = dup(fileno(stream));
close(duphandle);
}
参考资料:tc_search
@fflush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃
fflush(stdout)刷新标注输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上
@注:本文转自http://blog.chinaunix.net/u/32196/showart.php?id=250809
并非原创,收入本博,聊作记录.

C/C++ 误区:fflush(stdin) 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++
 
@2006-2-26 22:48 hxmzlgz
fflush(stdin);语句的作用

#include
#include
void main()
{float com=0.0,sale;int i;
char grade;
clrscr();
printf("/n Enter sale:");
scanf("%f",&sale);
fflush(stdin);
printf("/n Enter grade:");
scanf("%c",&grade);
if(sale>10000)
if(grade=='A')
com=sale*0.1 ;
else
com=sale*0.08 ;
else
com=sale*0.05;
printf("/n Com=%f",com);

}
请教:程序中的fflush(stdin);语句的作用是什么?如果不使用该语句却要实现sale和grade的连续输入要用什么方法?:(
2006-2-26 23:23 不简单
太高难度了!
2006-2-27 22:32 linsj6
fflush(stdin)关闭从stdin读的流,将流内内容读入缓冲区。fflush也可以关闭输出的流,将缓冲区内容写入流。
方法1)第二次输入改为scanf("/n%c",&grade);其中/n也可以用空格。
方法2)用getchar();插入原fflush(stdin)这一行忽略回车。
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值