我如何在这里冲洗STDIN? [英] How do I go about Flushing STDIN here?

查看:71
本文介绍了我如何在这里冲洗STDIN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数(用C语言编写)可以从用户那里获取输入,(使用scanf)将其存储在一个无符号的int中,然后将输入返回给其他处理该函数的函数:

I have a function (in C) that gets input from the user, (using scanf) stores it in an unsigned int, and returns the input to other functions that handle it:

unsigned
int input(void)
{
    unsigned int uin;

    scanf("%u", &uin);
    return val;
}

我想知道,由于我应该冲洗标准输入,我想使用getc相对于-vis的while循环:

I was wondering, being as I ought to flush stdin, I'd want to use a while loop using getc, vis-a-vis:

while (getc != '\n') {
   ...
}

但是,我不确定如何:

A)在getc内执行操作,例如我应该如何处理检查并将值连接到每个获得的字符,或者是否要使用getc,然后从那里进行连接,从而完全删除scanf.

A) Do operations inside of getc, as in how I ought to handle checks and concatenate the value, to each character gotten, or, whether or not I'm to getc, and then concatenate from there, removing scanf entirely.

B)这是否甚至是最合适的方法.

B) Whether this is even the most proper way to do this.

有什么好心的人可以给我一些提示和指示吗:)

Could some kind person give me some tips and pointers :)

谢谢.

推荐答案

我将%*[^\n]添加到scanf格式字符串的末尾. *表示它应该读取但忽略匹配的输入,而[^\n]表示除了换行符之外的任何内容,就像在正则表达式中一样.

I'd add %*[^\n] to the end of the scanf format string. The * means it should read but ignore the matching input, and the [^\n] means anything but a newline, just like in a regex.

这篇关于我如何在这里冲洗STDIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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