scanf / getchar序列问题 [英] scanf/getchar sequence problem

查看:80
本文介绍了scanf / getchar序列问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *

以下SGI上的代码会等你输入2件事,但是在Linux上它只会等待第一次。


我可以通过用以下内容替换scanf来使代码工作:


char数据[5];

fgets(数据, 5,stdin);

the_number = atoi(数据);


但是,我想修改scanf," / dev / tty" ,或者

简单的东西。

* /


#include< stdio.h>

int main()

{

int c;

int the_number;

fprintf(stderr, 输入数字:;;

scanf("%d"& the_number);

fprintf(stderr," \ n输入一封信: ");

c = getchar();

if((c = getchar())== EOF)

freopen(" / dev / tty"," r",stdin);

else

ungetc(c,stdin);

return(0) ;

}

/*
The below code on SGI will wait for you to enter 2 things, but
on Linux it will only wait the first time.

I can make the code work by replacing the scanf with:

char data [5];
fgets (data,5,stdin);
the_number = atoi (data);

But, instead I would like to modify the scanf, "/dev/tty", or
something simple like that.
*/

#include <stdio.h>
int main()
{
int c;
int the_number;
fprintf (stderr,"Enter Number :");
scanf ("%d",&the_number);
fprintf (stderr,"\nEnter a letter :");
c = getchar ();
if ( (c = getchar ()) == EOF )
freopen ("/dev/tty","r",stdin);
else
ungetc (c,stdin);
return (0);
}

推荐答案



cl ******** @ aol。 com 写道:
/ *
SGI上面的代码将等你输入2件事,但是在Linux上它只会在第一次等待。

我可以通过用以下内容替换scanf来使代码工作:

char数据[5];
fgets(data,5,stdin);
the_number = atoi(数据);

但是,我想修改scanf,/ dev / tty或
这样简单的东西。
* /

#include< stdio.h>
int main()
{
int c;
int the_number;
fprintf( stderr,输入数字:;;
scanf("%d",& the_number);
fprintf(stderr," \ n输入字母:");
c = getchar();
if((c = getchar())== EOF)
freopen(" / dev / tty"," r",stdin);
其他
ungetc(c,stdin);
retu (0);
}
/*
The below code on SGI will wait for you to enter 2 things, but
on Linux it will only wait the first time.

I can make the code work by replacing the scanf with:

char data [5];
fgets (data,5,stdin);
the_number = atoi (data);

But, instead I would like to modify the scanf, "/dev/tty", or
something simple like that.
*/

#include <stdio.h>
int main()
{
int c;
int the_number;
fprintf (stderr,"Enter Number :");
scanf ("%d",&the_number);
fprintf (stderr,"\nEnter a letter :");
c = getchar ();
if ( (c = getchar ()) == EOF )
freopen ("/dev/tty","r",stdin);
else
ungetc (c,stdin);
return (0);
}




Linux是对的; SGI错了。 scanf()应该消耗

任何初始空格,可选符号字符和

数字字符串,但不应使用以下''\ n'' (或

数字后面的其他内容)。随后的getchar()

应检索''\ n''(或其他)。


我的建议是避免scanf()进行交互输入:它

可能很难使用,并且从输入错误中恢复很困难(考虑如果用户输入#\\ n时会发生什么情况
第一次提示时
)。通常最好沿着治愈行进行。你发现:用

fgets()读取行,然后自己解释字符。使用strtod()或sscanf()比使用atoi()更好,因为

后者基本上没有*方法来诊断错误的输入。 br />

-
Er ********* @ sun.com



Linux is right; SGI is wrong. The scanf() should consume
any initial white space, an optional sign character, and a
digit string, but should not consume the following ''\n'' (or
whatever else follows the digits). The subsequent getchar()
should retrieve the ''\n'' (or whatever).

My advice is to avoid scanf() for interactive input: it
can be tricky to use, and recovery from input errors is
difficult (consider what happens if the user enters "#\n"
at your first prompt). It is usually better to proceed along
the lines of the "cure" you discovered: Read the line with
fgets() and then interpret the characters yourself. It''s
better to use strtod() or sscanf() than atoi(), because the
latter has essentially *no* way to diagnose erroneous input.

--
Er*********@sun.com


" Eric Sosman" < ER ********* @ sun.com>在消息中写道

news:d3 ********** @ news1brm.Central.Sun.COM
"Eric Sosman" <er*********@sun.com> wrote in message
news:d3**********@news1brm.Central.Sun.COM
cl ******** @ aol.com 写道:
/ *
以下SGI代码会等你输入两件事,但是在Linux上它只会在第一次等待。

我可以用以下代码替换scanf来使代码工作:

char data [5];
fgets(data,5,stdin);
the_number = atoi(数据);

但是,我想修改scanf ,/ dev / tty,或者像这样简单的东西。
* /

#include< stdio.h>
int main()
{
int c;
int the_number;
fprintf(stderr," Enter Number:");
scanf("%d",& the_number );
fprintf(stderr," \ n输入一个字母:");
c = getchar();
if((c = getchar())== EOF)
freopen(" / dev / tty"," r&qu ot;,stdin);

ungetc(c,stdin);
返回(0);
}
/*
The below code on SGI will wait for you to enter 2 things, but
on Linux it will only wait the first time.

I can make the code work by replacing the scanf with:

char data [5];
fgets (data,5,stdin);
the_number = atoi (data);

But, instead I would like to modify the scanf, "/dev/tty", or
something simple like that.
*/

#include <stdio.h>
int main()
{
int c;
int the_number;
fprintf (stderr,"Enter Number :");
scanf ("%d",&the_number);
fprintf (stderr,"\nEnter a letter :");
c = getchar ();
if ( (c = getchar ()) == EOF )
freopen ("/dev/tty","r",stdin);
else
ungetc (c,stdin);
return (0);
}



Linux是对; SGI错了。 scanf()应该消耗任何初始空格,一个可选的符号字符和一个
数字字符串,但不应该使用下面的''\ n''(或
其他任何内容)跟随数字)。随后的getchar()
应检索''\ n''(或其他)。



Linux is right; SGI is wrong. The scanf() should consume
any initial white space, an optional sign character, and a
digit string, but should not consume the following ''\n'' (or
whatever else follows the digits). The subsequent getchar()
should retrieve the ''\n'' (or whatever).




是的,但请注意有两个getchar( )在代码中调用,所以它应该可以在控制台输入一个字符(虽然效果不会是预期的一个字母)。


我想知道代码是否在Linux上运行而且在SGI上运行的代码真的是

相同。

-

John Carson



Yes, but note that there are two getchar() calls in the code, so it should
be possible to enter a char at the console (though the effect will not be
the intended one).

I wonder if the code run on Linux and the code run on SGI were really the
same.
--
John Carson





John Carson写道:


John Carson wrote:
" Eric Sosman < ER ********* @ sun.com>写了

[...]随后的getchar()
"Eric Sosman" <er*********@sun.com> wrote

[...] The subsequent getchar()
应该检索''\ n''(或其他)。
should retrieve the ''\n'' (or whatever).



是的,但请注意代码中有两个getchar()调用,因此它应该可以在控制台输入一个字符(虽然效果不会是预期的一个)。



Yes, but note that there are two getchar() calls in the code, so it should
be possible to enter a char at the console (though the effect will not be
the intended one).




哦,foozle。我想说那会告诉我不要用b / b来快速阅读代码。但是经验表明它不会......


-
呃********* @ sun.com



Oh, foozle. I''d like to say "That''ll teach me not
to speed-read code," but experience suggests it won''t ...

--
Er*********@sun.com


这篇关于scanf / getchar序列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆