【问题标题】:Why does an empty printf allow me to continue reading data from the stdin?为什么空的 printf 允许我继续从标准输入读取数据?
【发布时间】:2016-04-19 11:31:46
【问题描述】:

代码

while (1)
    {
        keycode = key_hook();
        if (keycode == SPACE || keycode == BKSPACE)
        {
            render_again = 1;
        }
        if (keycode == ESC)
            break;
        if (render_again)
        {
            render_again = 0;
            render(all);
        }
        dprintf(1, "");      //I have no idea why this prevents the program from freezing
    }
    int key_hook()
    {
     char buffer[4];

     read(0, buffer, 4);
     return (*(unsigned int *)buffer);
    }

好的,所以这段代码处理屏幕上文本的重绘。某些文本行使用termcaps (tputs(tgetstr("us, NULL")......) 加下划线或突出显示。一切都打印得很好,但是在第一次重绘文本之后,while 显然会冻结,除非存在 dprintf/printfkey_hook 函数只是从stdin 中读取4 bytes 并将它们转换为int

【问题讨论】:

  • 发布key_hook 函数可能是个好主意...
  • stdin 中的 4 个字符如何等同于按键?我假设这个程序不是从 tty 读取的?
  • 是的,但我需要 4 个字符用于特殊键,例如箭头键,例如这样的代码 ^[[C
  • 不确定我是否关注。我认为^[ESC 的视觉表示...
  • 从终端读取时,箭头键由 4 个字符组成,这是我从测试中扣除的,它工作正常,直到我引入了冻结它的 termcaps 部分

标签: c terminal termcap


【解决方案1】:

当我上次在这里工作时,我的 key_hook 版本有一个单字节读取循环。这被 1 秒的警报和到目前为止的数据是否是键前缀的逻辑打破了。

闹钟中断读取,停止冻结

【讨论】:

    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多