【发布时间】:2014-03-31 08:20:22
【问题描述】:
此代码不起作用:
_tprintf(TEXT("Enter password or press enter to skip: "));
pszPassword = new TCHAR[100];
int numFields = _tscanf_s(TEXT("%s"), pszPassword, 100);
if (numFields == 0) // never reached
{
delete[] pszPassword;
pszPassword = NULL;
}
按 Enter 不会使 scanf 中止解析输入,因为它会跳过空格,直到找到非空格字符。
我怎样才能实现所需的行为?
该程序实际上是 C 语言,我使用 new 和 delete 而不是 malloc 但不想使用 std::string 等。
【问题讨论】:
-
不要对字符串进行 scanf。使用 fgets 等。
-
有 C 风格的方法吗?
标签: c windows input console scanf