It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
使用此代码,当我接受输入时,输入不会出现,当我将其注释掉时,该代码在下面做什么,

struct termios origConfig;
tcgetattr(0, &origConfig);
struct termios newConfig = origConfig;
newConfig.c_lflag &= ~(ICANON|ECHO);
newConfig.c_cc[VMIN] = 1;
newConfig.c_cc[VTIME] = 1;
tcsetattr(fileno(stdin), TCSANOW, &newConfig);

最佳答案

newConfig.c_lflag &= ~(ICANON|ECHO);


将重置echo和规范输入标志。如果您不想重置echo标志(但保留其他所有内容),请使用:

newConfig.c_lflag &= ~(ICANON);


尽管您可能还想检查一下规范输入标志的工作原理。例如,请参见Linux termios man page

在任何情况下,不使用其他选项可能是不明智的,因为非规范输入的主要用例之一是诸如编辑器之类的东西,在这种情况下,您不希望字符回显。

非斜线输入为您提供了键入时的每个字符(而不是按ENTER键时显示的整个行),非常适合编辑者。如果您的编辑器需要处理CTRL-W之类的操作以向前移动一个单词,则可能不希望它在屏幕显示的中间打乱^W

相反,您希望它将输出留给程序,而不是终端接口。非规范的,非回声的终端I / O通过接口为您提供了更多功能。

关于c - struct termios在C中的未知行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15354097/

10-12 16:12
查看更多