新行字符:scanf和getchar [英] new line character : scanf and getchar

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

问题描述

char c;

int i;


scanf("%d",& i);

c = getchar();


我想读取一个整数和一行2行。

为什么getchar总是从中得到\ n字符最后一行?????


(对不起我的新手问题,但

新闻组的FAQ部分在哪里?)

解决方案



" Mars" <火星@火星>在消息新闻中写道:41 ********** @ rain.i-cable.com ...

char c;
int i;

scanf("%d",& i);
c = getchar();

我想读取一个整数和一行2行。
为什么getchar总是从最后一行得到\ n字符?????


在你的情况下,stdin指的是一个交互式设备,因此,就是行

缓冲。

这意味着当遇到换行符时会传输数据。


如果scanf的转换成功,你将拥有输入中留下一个换行符

stream。

stdin上发出的任何后续读取都会遇到这个换行符

之后没有输入其他数据一些整数,对应你的

转换

说明符。


一个解决方案就是,


void consume_stdin(void)

{

while((getchar())!=''\ n'')

;

}

您还应该对scanf的返回值进行一些错误检查。

(对不起我的新手问题,但FAQ的部分在哪里
新闻组??)



http://www.eskimo.com/~scs/C-faq/faq.html


-

j


为什么你不只是写


int i;

char c;


scanf("%d",& i);

fflush(stdin); //刷新输入流并删除''\ n''

字符

c = getchar();


尝试一下,它会起作用。




" Sontu" < AB ****** @ gmail.com>在消息中写道

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

为什么不写下来?

char c;

scanf("%d",& i) ;
fflush(stdin); //刷新输入流并删除''\ n''
字符
c = getchar();

尝试一下,它会起作用。




假设输入流中有数据,你建议将它刷新到哪个


这实际上是在FAQ中解决的,但我想让你回答我的问题。


-

j


char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????

(Sorry for my newbie question, but where is the FAQ section of the
newsgroup??)

解决方案


"Mars" <Mars@Mars> wrote in message news:41**********@rain.i-cable.com...

char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????

In your case, stdin refers to an interactive device and as such, is line
buffered.
This means that data is transferred when a newline is encountered.

If scanf''s conversion succeeds you will have a newline left in your input
stream.
Any subsequent read issued on stdin will encounter this newline if you did
not enter additional data after some integer that corresponds to your
conversion
specifier.

One solution would be,

void consume_stdin(void)
{
while((getchar()) != ''\n'')
;
}
You should also do some error checking with respect to scanf''s return value.
(Sorry for my newbie question, but where is the FAQ section of the
newsgroup??)



http://www.eskimo.com/~scs/C-faq/faq.html

--
j


Why don''t you just write

int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of ''\n''
character
c=getchar();

Try it out, it will work.



"Sontu" <ab******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

Why don''t you just write

int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of ''\n''
character
c=getchar();

Try it out, it will work.



Supposing that there is data in the input stream, where do you propose
that it will be flushed to?

This is actually addressed in the FAQ but I want you to answer my question.

--
j


这篇关于新行字符:scanf和getchar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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