C |如何检查我的输入缓冲区(stdin)是否为空? [英] C| how to check if my input buffer(stdin) is empty?

查看:1122
本文介绍了C |如何检查我的输入缓冲区(stdin)是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何检查我的输入缓冲区(也许称为stdin)是否为空。
i不希望程序在缓冲区为空的情况下停止,并且我也不希望输入必须以'\n'结尾,因此仅使用scanf是不够的。

I want to know how to check if my input buffer(perhaps its called stdin) is empty or not. i dont want the program to stop if the buffer is empty, and i dont want the input to necessarily end with '\n', therefore just using scanf is not enough.

我尝试在Google和此网站上进行搜索,但答案还不够。
i尝试使用feof(stdin)像这样:

i tried searching on google and on this website but no answer was enough. i tried using feof(stdin) like this:

int main()
{
char c,x;
int num;
scanf("%c",&c);
scanf("%c",&x);
num=feof(stdin);
printf("%d",num);
}

但是无论输入如何,它所做的只是打印0。在第二个scanf之后添加fflush(stdin)可以得到相同的结果。
其他答案建议使用选择和民意测验,但我找不到这些功能的任何解释。
其他论坛告诉我使用getchar(),但我认为他们误解了我的问题。

but all it did was printing 0 no matter the input. adding fflush(stdin) after the second scanf gave the same result. other answers suggested using select and poll but i couldnt find any explanations for those functions. some other forum told me to use getchar() but i think they misunderstood my question.

在Google尝试的Google搜索中:C如何检查输入缓冲区空,C标准输入为空,C输入缓冲区检查为空。
这是一个普遍的问题,它不是针对特定代码的,因此我为什么需要它并不重要。

on the google search i tried: C how to check input buffer empty, C stdin empty, c "input buffer" check empty. this is a general question, its not for a specific code so it doesnt matter why i need it.

**如果您建议我使用选择/投票,能否请您添加一个有关如何使用它们的解释?

**if you suggest i use select/poll, could you please add an explanation about how to use those?

推荐答案

以下是解决此问题的代码:

Here is the code for solving this:

fseek (stdin, 0, SEEK_END);
num = ftell (stdin);

fseek 将指针放在末尾 stdin 输入缓冲区的值。 ftell 将返回文件的大小。

fseek will put the pointer at the end of the stdin input buffer. ftell will return the size of file.

这篇关于C |如何检查我的输入缓冲区(stdin)是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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