setvbuf用来无法使标准输入缓冲 [英] setvbuf not able to make stdin unbuffered

查看:364
本文介绍了setvbuf用来无法使标准输入缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要目的是让的getchar 一旦它得到的不是等待<大骨节病> ENTER 键的字符返回。我想这

My main intention was to make getchar return as soon as it gets a character instead of waiting for the ENTER key. I tried this

int main()
{
    setvbuf(stdin,NULL,_IONBF,0);
    getchar();

    return 0;
}

setvbuf用来

setvbuf ( FILE * stream, char * buffer, int mode, size_t size );

应该设置标准输入来缓冲模式。

但还是的getchar()一直等待<大骨节病> ENTER

我看过相关的帖子是这样

I've seen related posts like this

<一个href=\"http://stackoverflow.com/questions/10228902/printing-while-reading-characters-in-c/10229044#10229044\">Printing而读取字符用C

这是建议的替代方法,使标准输入缓冲。但我很好奇地想知道,为什么 setvbuf用来方法不起作用。

which are suggesting alternate methods to make stdin unbuffered. But I am curious to know as to why setvbuf method does not work

推荐答案

直到你回车,终端驱动程序不返回任何东西,即使阅读()操作会接受什么已经存在。

The terminal driver doesn't return anything until you hit return, even if the read() operation would accept what's already there.

从终端获取字符逐个字符输入,你必须把它弄出来的标准模式下为原始或CBREAK模式,这完全需要不同的操作。看看在通用终端接口'的POSIX手册如何控制终奌站。或考虑使用诅咒库。

To get character-by-character input from a terminal, you have to get it out of canonical mode into raw or cbreak mode, and that requires different operations altogether. Take a look at the POSIX manual on 'General Terminal Interface' for how to control the terminal. Or consider using the curses library.

参见:<一href=\"http://stackoverflow.com/questions/358342/canonical-vs-non-canonical-terminal-input/358381#358381\">Canonical

这篇关于setvbuf用来无法使标准输入缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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