【发布时间】:2019-01-18 09:56:28
【问题描述】:
当我使用 getch 时,它总是在读取的字符后面附加一个空字符。
当我使用以下代码时:
#include "stdafx.h"
#include <conio.h>
int main()
{
char c = 0;
while (c != 'x')
{
c = _getch();
printf("Char read: <%c> \n", c);
}
}
当我按下“asdx”键时,它会在控制台上返回以下内容:
Char read: <a>
Char read: < >
Char read: <s>
Char read: < >
Char read: <d>
Char read: < >
Char read: <x>
这是在 VS 2017 中编译的一个普通的新单文件项目,在 Windows 10 控制台窗口上运行。我尝试删除 _UNICODE 和 UNICODE 定义。
【问题讨论】:
-
你怎么知道它是一个空字符?顺便说一句,
_getch()返回int。 -
按预期工作,您是否尝试打印 c 的数值以查看它是什么?
-
我知道它是一个空值,因为我将它追踪到调试器中。我尝试使用 int 或 char,得到了相同的结果。
-
这显然是某些版本的 Windows SDK 的问题,请参阅developercommunity.visualstudio.com/content/problem/247770/…
-
@AlanBirtles:它在调试模式下对我有用,但在发布模式下失败。与 OP 的行为相同。
标签: c++ visual-studio-2017 console-application