为什么getchar像缓冲区一样工作,而不是实时工作 [英] Why does getchar work like a buffer instead of working as expected in real-time

查看:177
本文介绍了为什么getchar像缓冲区一样工作,而不是实时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对stackoverflow的第一个问题。请原谅我,如果我没有正确搜索,但我似乎没有找到解释这一点。只是试图从Bjourne Stroustroup的文章的一个例子。在我键入文本时,添加了我的位来查看数组的大小。



但是它似乎不工作了! getchar()只是等待,直到我完成输入所有的字符,然后它将执行循环。根据逻辑,它实际上不进入循环,获取一个字符,执行其操作,然后迭代。我想知道这是否是实现特定的,或打算像这样?



我在Ubuntu 14.04 LTS使用codeblocks与gcc 4.8.2。

  while(true)
{
int c = getchar();
if(c =='\\\
'|| c == EOF)
{
text [i] = 0;
break;
}
text [i] = c;

if(i == maxsize-1)
{
maxsize = maxsize + maxsize;
text =(char *)realloc(text,maxsize);
if(text == 0)exit(1);
cout<< \\\
将数组大小增加为<< maxsize<< endl;
}

i ++;
}

输出如下:



Array Size现在是:10
请输入一些文本:这是一些示例文本。我希望看到的内存正在这里,但显然这不是它的工作原理!



将数组大小增加到20



将数组大小增加到40



将数组大小增加到80



160



您输入了:这是一些示例文本。我希望看到的内存正在这里,但显然这不是它的工作原理!



数组大小现在是:160

解决方案

这直接与 getchar 无关。 问题是底层终端,它将缓冲您的输入。按Enter键后,输入将发送到程序。在Linux(dunno如果有一种方式在Windows)你可以解决这个通过调用

  / bin / stty raw 

 

code> system(/ bin / stty raw);

。这将导致getchar立即向您返回输入字符。



不要忘记通过调用重置 tty 行为

  / bin / stty cooked 

$ b $


$ b

下面是一个示例(适用于Linux):

  #include< stdio.h> 
#include< stdlib.h>

using namespace std;

int main(){
system(/ bin / stty raw);
char c = getchar();
system(/ bin / stty cooked);
return 0;
}

还有一个看看这个SO Post:如何避免按任何getchar()键回车



此外,如评论中所建议,请查看这里: http:// linux.die.net/man/3/termios ,特别是命令 tcsetattr ,它应该在跨平台工作。


This is my first question on stackoverflow. Pardon me if I haven't searched properly but I do not seem to find an explanation for this. Was just attempting an example from Bjourne Stroustroup's papers. Added my bits to see the array get re-sized as I type the text.

But it doesn't seem to work that way! getchar() simply waits till I am done with entering all the characters and then it will execute the loop. As per the logic, it doesn't actually go into the loop, get a character, perform its actions and then iterate. I am wondering if this is implementation specific, or intended to be like this?

I am on Ubuntu 14.04 LTS using Codeblocks with gcc 4.8.2. The source was in cpp files if that matters.

while(true)
{
    int c = getchar();
    if(c=='\n' || c==EOF)
    {
        text[i] = 0;
        break;
    }
    text[i] = c;

    if(i == maxsize-1)
    {
        maxsize = maxsize+maxsize;
        text = (char*)realloc(text,maxsize);
        if(text == 0) exit(1);
        cout << "\n Increasing array size to " << maxsize << endl;
    }

    i++;
}

The output is as follows:

Array Size is now: 10 Please enter some text: this is some sample text. I would have liked to see the memory being realloced right here, but apparently that's not how it works!

Increasing array size to 20

Increasing array size to 40

Increasing array size to 80

Increasing array size to 160

You have entered: this is some sample text. I would have liked to see the memory being realloced right here, but apparently that's not how it works!

Array Size is now: 160

解决方案

This has nothing to do with getchar directly. The "problem" is the underlying terminal, which will buffer your Input. The Input is sent to the program after you press enter. In Linux (dunno if there is a way in Windows) you can workaround this by calling

/bin/stty raw

in terminal or by calling

system ("/bin/stty raw");

in your program. This will cause getchar to immediately return the input character to you.

Dont forget to reset the tty behaviour by calling

/bin/stty cooked

when done!

Here is an example (for Linux):

#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main() {
    system ("/bin/stty raw");
    char c = getchar();
    system ("/bin/stty cooked");
    return 0;
}

Also have a look at this SO Post: How to avoid press enter with any getchar()

Also, as suggested in the comments, have a look here: http://linux.die.net/man/3/termios especially on the command tcsetattr, which should work cross-platform.

这篇关于为什么getchar像缓冲区一样工作,而不是实时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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