且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

齐平(标准输入)

更新时间:2023-11-17 23:35:16



" Darklight" <纳克****** @ netscape.net>写了

"Darklight" <ng******@netscape.net> wrote

这是正确的还是不正确的我只是在发布之前阅读下面的帖子
这个:在fflush(stdin)中,刷新数据会发生什么?

fflush()没有为输入流定义。它可以什么都不做,它可能会导致报告错误,或者它可以清除等待的缓冲区

字符。
这个程序取自sams在21天内自学c

/ * LIST13-6.C清除额外字符的标准* /
/ *使用FFLUSH()功能* /
#include< stdio。 h>

int main(无效)
{年龄;
字符名称[20];

/ *用户提示''年龄* /
放(输入年龄:);
scanf("%d"& age);

/ * CLEAR STDIN任何额外的角色* /
fflush(stdin);

/ *提示用户姓名* /
put(输入你的名字:);
scanf("%s",name);

/ *显示数据* /
printf(你的年龄是%d \ n,年龄);
printf(你的名字是%s \ n,名字);

返回0;
} fflush功能不能正常工作。

这本书不是很好,因为它教的是坏习惯。
上面的程序是否会在windows中工作
如果我用刷新功能替换

void clear_kb(void)

/ *清除任何等待的STDIN字符* /
{junk [80];
fgets(垃圾,80,stdin);


上述程序可行

is this correct or incorrect i just read the post below before posting
this: In fflush(stdin), what happens to flushed data?
fflush() isn''t defined for input streams. It could do nothing, it could
cause an error to be reported, or it could clear the buffer of waiting
characters.
this program is taken from sams teach yourself c in 21 days

/* LIST13-6.C CLEARING STDIN OF EXTRA CHARACTERS */
/* USING FFLUSH() FUNCTION */
#include<stdio.h>

int main(void)
{
int age;
char name[20];

/* PROMPT FOR USER''S AGE */
puts("Enter age: ");
scanf("%d",&age);

/* CLEAR STDIN OF ANY EXTRA CHARACTERS */
fflush(stdin);

/* PROMPT FOR USER''S NAME */
puts("Enter your name: ");
scanf("%s",name);

/* DISPLAY DATA */
printf("Your age is %d\n",age);
printf("You name is %s\n",name);

return 0;
}

my os is suse 9.1 pro
the fflush function dosn''t work.
The book isn''t very good, since it is teaching bad habits.
would the above program work in windows
if i replace the flush function with

void clear_kb(void)

/* CLEARS STDIN OF ANY WAITING CHARACTERS */
{
char junk[80];
fgets(junk,80,stdin);
}

the above program works



不幸的是,如果没有字符

等待,所有输入函数都会阻塞。所以调用fgets()并不是一个特别好的解决方案。


Unfortunately, all the input functions block if there is no character
waiting. So calling fgets() isn''t a particularly good solution.


Darklight< ng ****** @ netscape.net>写道:
Darklight <ng******@netscape.net> writes:
这是正确还是不正确我只是在发布之前阅读下面的帖子
这个:在fflush(stdin)中,刷新数据会发生什么?


fflush(stdin)产生未定义的行为。来自常见问题解答:


12.26:我如何刷新待处理的输入,以便用户的预先输入不是在下次提示时读取
? fflush(stdin)会工作吗?


答:fflush()仅针对输出流定义。由于其刷新的定义为
是完成缓冲的

字符的写入(不丢弃它们),丢弃未读输入将对输入流上的fflush不是类似的意思。


没有标准的方法可以从

stdio输入流中丢弃未读的字符,也不会这样的方式必然是
足够,因为未读字符也可以累积在其他OS级别的输入缓冲区中。您可以阅读和/或
丢弃字符直到\ n,或使用curses flushinp()

函数,或使用某些系统特定的技术。另见

问题19.1和19.2。


参考文献:ISO Sec。 7.9.5.2; H& S Sec。 15.2。

这个程序来自于21天内自学c的自学c
is this correct or incorrect i just read the post below before posting
this: In fflush(stdin), what happens to flushed data?
fflush(stdin) yields undefined behavior. From the FAQ:

12.26: How can I flush pending input so that a user''s typeahead isn''t
read at the next prompt? Will fflush(stdin) work?

A: fflush() is defined only for output streams. Since its
definition of "flush" is to complete the writing of buffered
characters (not to discard them), discarding unread input would
not be an analogous meaning for fflush on input streams.

There is no standard way to discard unread characters from a
stdio input stream, nor would such a way necessarily be
sufficient, since unread characters can also accumulate in
other, OS-level input buffers. You may be able to read and
discard characters until \n, or use the curses flushinp()
function, or use some system-specific technique. See also
questions 19.1 and 19.2.

References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.
this program is taken from sams teach yourself c in 21 days




然后你应该扔掉那本书。这是一个威胁。

-

有些人*是*傲慢,其他人阅读常见问题。

- Chris Dollin



Then you should throw away that book. It is a menace.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin


Darklight写道:
Darklight wrote:
这是正确还是不正确我只是在发布之前阅读下面的帖子
这个:在fflush(stdin)中,刷新数据会发生什么?


谁知道? fflush没有为输入流定义。
这个程序是在21天内从sams自学c中获取的
is this correct or incorrect i just read the post below before posting
this: In fflush(stdin), what happens to flushed data?
Who knows? fflush is not defined for input streams.
this program is taken from sams teach yourself c in 21 days




所以烧掉这本书。



So burn the book.