关于fflush的作用问题

sfesly 2011-06-28 12:11:57
有这样一行输入void main()
{
char a[10],b;
int h;
scanf("%s",a);
fflush(stdin);
scanf("%c",&b);
scanf("%d",&h);
printf("%s %c %d ",a,b,h);
system("pause");
}
加上fflush之前,无法正常输入,但加上fflush之后似乎只有使用回车键才能结束一个scanf,空格全部没用了,这是为什么?
...全文
464 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
SVKING 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 ljljlj 的回复:]

scanf(" %c",&b);
切记在%c前加个空格,它会自动跳过空白和换行符,这才是C标准做法。
[/Quote]
学习了!
coldicy2007 2011-07-03
  • 打赏
  • 举报
回复
[Quote=引用楼主 sfesly 的回复:]
scanf("%s",a);
fflush(stdin);
scanf("%c",&b);
scanf("%d",&h);

楼上有人说fflush()对stdin未定义,scanf()刚好是从stdin获取数据的吧,那这里的fflush()不是多余了??
desdouble 2011-07-03
  • 打赏
  • 举报
回复
In general, scanf and friends are not highly regarded; this is for three reasons:

❑ Traditionally, the implementations have been buggy.

❑ They’re inflexible to use.

❑ They lead to code where it’s very difficult to work out what is being parsed.

Try to use other functions, like fread or fgets, to read input lines and the string functions to break the
input into the items you need.
pathuang68 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 anyidan 的回复:]

int fflush(FILE *stream)
On an output stream, fflush causes any buffered but unwritten data to be written; on an input stream, the effect is undefined. It returns EOF for a write error, and zero oth……
[/Quote]

说得好。

fflush仅针对输出流有效,对于输入流而言,其行为是undefined的。
_yyy 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 ljljlj 的回复:]

scanf(" %c",&b);
切记在%c前加个空格,它会自动跳过空白和换行符,这才是C标准做法。
[/Quote]

mark 学习了
_yyy 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 hpsmouse 的回复:]

引用 12 楼 zhao4zhong1 的回复:

改用rewind(stdin);

这个比 fflush 还要不靠谱。
[/Quote]

i think too
子庚 2011-06-30
  • 打赏
  • 举报
回复
顶帮你顶一下。
辰岡墨竹 2011-06-30
  • 打赏
  • 举报
回复
哎,不知道楼主是看了哪本误人子弟的书。
因为scanf("%s",a);是行缓冲的函数,它必须以回车作为输入的结束。
如果你不用回车结束第一个scanf(),而是用空格,那么多输入的字符在被之后的scanf接收前,就被清除缓冲区了(fflush(stdin)),不过C标准只规定了标准输出和普通文件可以fflush,stdin的fflush的效果是未定义的。
还有void main()也是错误的。C标准只允许main()(省略返回值相当于int,C99不允许,C89允许)和int main(),根本没有void main()这种形式。不过微软也犯过这个错误,当年Microsoft C/C++库的官方参考书里所有例子都是void main(),所以VC能容许这种错误。
zero_to 2011-06-30
  • 打赏
  • 举报
回复
我也关注这个问题
Jesusgospelnj 2011-06-30
  • 打赏
  • 举报
回复
刷新缓冲区哦,将缓冲区中的内容push出去
ljhhh0123 2011-06-30
  • 打赏
  • 举报
回复
scanf(" %c",&b);
切记在%c前加个空格,它会自动跳过空白和换行符,这才是C标准做法。
2011-06-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zhao4zhong1 的回复:]

改用rewind(stdin);
[/Quote]
这个比 fflush 还要不靠谱。
bailangtaotian 2011-06-29
  • 打赏
  • 举报
回复
第一次输入的是字符串,当空格、制表符以及回车换行出现后,就是一个字符串。如果你输入的一串字符之间有上面提到的字符时,自动截取空格、tab、或是enter前面的字符串作为第一次的输入。但后面还有字符串,那就留在了内存缓冲区中,以后不管你再输入什么字符或是字符串,读取的顺序都是先来后到的。你后面还有两次输入,那就从内存缓冲区中再取两个字符,前面一个赋值给char型变量,后面一个字符转化为int类型赋值给h。
金刚葫芦娃 2011-06-29
  • 打赏
  • 举报
回复
悲催的程序人生.
2011-06-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 anyidan 的回复:]
标准说此函数对 input stream 的行为未定义,为何大家都在用?
[/Quote]
因为微软把这个未定义的东西给猥琐地定义了……

fflush(stdin) 拿到其他编译器下面基本都会悲剧……
AnYidan 2011-06-29
  • 打赏
  • 举报
回复
int fflush(FILE *stream)
On an output stream, fflush causes any buffered but unwritten data to be written; on an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.


标准说此函数对 input stream 的行为未定义,为何大家都在用?
赵4老师 2011-06-29
  • 打赏
  • 举报
回复
改用rewind(stdin);
KPRF2009 2011-06-29
  • 打赏
  • 举报
回复
清空缓冲区
如此美丽的你 2011-06-29
  • 打赏
  • 举报
回复
fflush
Flushes a stream.

Function Required Header
fflush <stdio.h>


int fflush( FILE *stream );
Parameters
stream
Pointer to FILE structure
Libraries
All versions of the C run-time libraries.

Return Values
fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error.

Note If fflush returns EOF, data may have been lost due to a write failure. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function.

Remarks
The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream.

Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream.

For information about controlling the commit-to-disk feature, see Stream I/O, and fopen.

Example
/* FFLUSH.C */

#include <stdio.h>
#include <conio.h>

void main( void )
{
int integer;
char string[81];

/* Read each word as a string. */
printf( "Enter a sentence of four words with scanf: " );
for( integer = 0; integer < 4; integer++ )
{
scanf( "%s", string );
printf( "%s\n", string );
}

/* You must flush the input buffer before using gets. */
fflush( stdin );
printf( "Enter the same sentence with gets: " );
gets( string );
printf( "%s\n", string );
}

Output
Enter a sentence of four words with scanf: This is a test
This
is
a
test
Enter the same sentence with gets: This is a test
This is a test

無_1024 2011-06-28
  • 打赏
  • 举报
回复
刷新缓冲区
加载更多回复(5)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧