[英]How the code gets to the next step only when i press the enter key in c++ (Thread)
I saw the following code on the internet as 我在互联网上看到以下代码
DWORD qThreadID;
HANDLE hThread = CreateThread(0, 0, ThreadFn, &uiCounter, 0, &qThreadID);
// Loop until the user enters 'q'
char cChar = ' ';
while (cChar != 'q') {
cout << uiCounter << endl;
cChar = (char)getchar();
}
how does the keypress event "Enter" works on it? 按键事件“ Enter”如何工作? (when i debug it except for the press of "Enter" no other keypress functionality works ) Thanks
(当我调试它时,除了按下“ Enter”键外,其他按键功能均无效)
getchar()
reads from standard in, which is buffered, both in the library and in the OS. getchar()
从库和操作系统中的标准缓冲中读取。 The usual OS's won't return from a read on a console device until enter is entered; 在输入enter之前,通常不会从控制台设备上的读取返回操作系统。 they support command line editing, and require the enter key to finalize the input.
它们支持命令行编辑,并且需要Enter键来完成输入。
getchar() reads a single character of input. getchar()读取输入的单个字符。
However, your terminal likely does line buffering on the input, no input is sent to your program until you hit enter. 但是,您的终端可能会在输入上进行行缓冲,直到您按下Enter键,才将输入发送到程序。
因为enter
输入一个\\n
,它将被解释为EOF
因为命令行会认为这是用户输入的结尾,并且getChar()
试图从该流中读取单个字符缓冲区,因此如果您只是按enter
而不返回任何内容,在其前面输入字符,请参阅msdn: http : //msdn.microsoft.com/zh-cn/library/5231d02a%28v=vs.71%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.