Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在stdin上读取()返回EOF而不是等待输入_C_Stdin_Termios - Fatal编程技术网

C 在stdin上读取()返回EOF而不是等待输入

C 在stdin上读取()返回EOF而不是等待输入,c,stdin,termios,C,Stdin,Termios,有人知道为什么运行下面的代码可能会导致该fd(即stdin)上所有未来的read()调用立即返回0,而不是阻塞输入吗 termios newTerminalSettings; tcgetattr(inFd, &newTerminalSettings); newTerminalSettings.c_lflag &= ~ICANON; tcsetattr(inFd, TCSANOW, &newTerminalSettings); 删除tcsetattr行可以使read()按

有人知道为什么运行下面的代码可能会导致该fd(即stdin)上所有未来的read()调用立即返回0,而不是阻塞输入吗

termios newTerminalSettings;
tcgetattr(inFd, &newTerminalSettings);
newTerminalSettings.c_lflag &= ~ICANON;
tcsetattr(inFd, TCSANOW, &newTerminalSettings);
删除tcsetattr行可以使read()按预期工作

还尝试:

fcntl(inFd, F_SETFL, 0);
没有运气

请注意,我目前有两个不同的终端。在其中一个应用程序中运行应用程序会导致read立即返回。在其他模式下运行它会导致读取阻塞输入。可能是什么

提前感谢:-)

复制源:

#include <iostream>
#include <termios.h>

using namespace std;

int main(void) {
    termios newTerminalSettings;
    tcgetattr(0, &newTerminalSettings);
    newTerminalSettings.c_lflag &= ~ICANON;
    tcsetattr(0, TCSANOW, &newTerminalSettings);

    char readBuf[5000];
    cout << "read returned: " << read(0, readBuf, sizeof(readBuf));

    return 0;
}
#包括
#包括
使用名称空间std;
内部主(空){
终端设置;
tcgetattr(0和新终端设置);
newTerminalSettings.c_lflag&=~ICANON;
tcSetTTR(0、TCSANOW和newTerminalSettings);
char readBuf[5000];

cout我认为您的问题在于屏蔽了ICANON,而ICANON反过来会关闭规范模式(启用非规范模式)。根据termios(3)的手册页:

在非规范模式下,输入立即可用(用户无需键入行分隔符),并且行编辑被禁用

为了避免这篇文章混乱,请参阅手册页,因为它详细解释了这种行为

来自toptal工程公司的Gergely

请记住,tty驱动程序维护一个字节的输入队列 已从串行线读取,但未传递给用户,因此 每个read()调用都会等待实际的I/O—读操作可能非常简单 直接从输入队列中满足

提及