C getchar 与 scanf
我对我正在研究的函数中发现的一段代码感到困惑:
char GetCommand( void )
{
char command;
do {
printf( "Enter command (q=quit, n=new, l=list): " );
scanf( "%c", &command );
Flush();
}
while ( (command != 'q') && (command != 'n')
&& (command != 'l') );
printf( "\n----------\n" );
return( command );
}
void Flush( void ) {
while ( getchar() != '\n' )
;
}
我不太明白的是 Flush()
函数的用法。我的意思是,我正在阅读的书解释说,它可以防止用户输入多个字符,然后在提示他们第二次输入时读取该字符。
我不明白的是 Flush()
如何防止这种情况发生。它没有任何作用。它只是一个 while
命令。 (虽然这是真的……什么??????)没有意义。
I am confused by a piece of code found in a function I am studying:
char GetCommand( void )
{
char command;
do {
printf( "Enter command (q=quit, n=new, l=list): " );
scanf( "%c", &command );
Flush();
}
while ( (command != 'q') && (command != 'n')
&& (command != 'l') );
printf( "\n----------\n" );
return( command );
}
void Flush( void ) {
while ( getchar() != '\n' )
;
}
What I don't quite understand here is the usage of the Flush()
function. I mean, the book I am reading explains it by saying that it prevents the user from inputting more than a single character and then having that character read when they are prompted for input the 2nd time.
What I don't understand is how Flush()
is preventing this from happening. It doesn't DO anything. All it is is a while
command. (While this is true......what?????) Doesn't make sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getchar()
具有从输入缓冲区中删除下一个字符的副作用。Flush
中的循环读取并丢弃字符,直到换行符\n
结束该行。由于
scanf
被告知读取一个且仅一个字符 (%c
),这会导致忽略该输入行上的所有其他内容。如果将 scanf 替换为,可能会更清楚
,但它实际上是一个通常不好的例子,因为它不能很好地处理文件结束。
一般来说,最好忘记
scanf
;fgets
和sscanf
工作得更好,因为一个负责获取输入,另一个负责解析它。scanf
(和fscanf
)尝试一次执行太多作业。getchar()
has the side effect of removing the next character from the input buffer. The loop inFlush
reads and discards characters until - and including - the newline\n
ending the line.Since the
scanf
is told to read one and only one character (%c
) this has the effect of ignoring everything else on that input line.It would probably be more clear if the scanf was replace with
but it's actually a generally bad example as it does not handle End Of File well.
In general
scanf
is best forgotten;fgets
andsscanf
work much better as one is responsible for getting the input and the other for parsing it.scanf
(andfscanf
) try to do too many jobs at once.getchar
从标准输入读取一个字符。如果将其放入while
循环中,它将继续一次读取一个字符,直到条件为 false。Flush
函数正在执行读取操作,直到遇到换行符 (\n
)。这是当用户按下回车键时产生的字符。因此,您给出的代码将读取一个字符(我不清楚为什么它使用
scanf
来代替简单的getchar
,这样会更快),然后丢弃其余的输入,直到用户按下回车键。如果您要向此程序提供
foobar
,它将在Flush
函数中获取f
并丢弃oobar
。如果不调用flush
,f
可以转到一个scanf
,第二个scanf
将获取第一个scanf
> o 。getchar
reads one character from standard input. If you put it in awhile
loop, it will continue to read one character at a time until the condition is false.What the
Flush
function is doing is reading until it encounters a newline (\n
). This is the character produced when the user hits the enter key.So, the code you gave will read one character (I'm unclear on why it uses
scanf
for this instead of simplygetchar
, which would be faster), and then discards the rest of the input until the user hits enter.If you were to feed this program
foobar
, it would take thef
and discard theoobar
in theFlush
function. Without callingflush
, thef
could go to onescanf
, and the secondscanf
would get the firsto
.当您输入字符并按 Enter 键时,按 Enter 键会生成换行符,并将其保留在缓冲区中。这是有问题的,因为它将等到您下次需要用户输入时才将其用于该输入。 Flush 用于从输入缓冲区中刷新换行符,这样就不会出现这个问题。 Flush实际上在读取时使用了输入缓冲区中的换行符,并且将其丢弃,因此它不再位于缓冲区中。
When you enter your character and press Enter, a newline character is generated by you pressing the Enter key and it remains in the buffer. This is problematic because it will wait around until the next time you require user input and it will be used for that input. Flush is used to flush the newline character from the input buffer so you don't have that problem. Flush actually uses the newline in the input buffer when it reads it and it is discarded, so it is no longer in the buffer.