为什么一个空的printf允许我继续从stdin读取数据?

Rpr*_*eda 5 c terminal termcap

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);
    }
Run Code Online (Sandbox Code Playgroud)

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

mks*_*eve 1

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

警报中断读取,并停止冻结