使用 istream (std cin):防止在 Windows 上"[input] is not recognized as ..."
Using istream (std cin): prevent "[input] is not recognized as ..." on Windows
我有一个具有命令行功能的Qt-gui应用程序。为了实现这一点,我将其添加到main()
函数的顶部:
#ifdef _WIN32
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
freopen("CONOUT$", "w", stdout);
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stderr);
}
#endif
然后构造了我的主类的一个实例。在构造函数中,QCommandLineParser
确定是否有任何参数,并创建cmdline解析类或gui应用程序类的实例。
在cmdline解析类中,我要求用户输入某些值:
QString qanswer;
// `answerToInt` is an std::unordered_map
while (answerToInt.find(qanswer) == answerToInt.end()) {
std::cout << std::endl << "File will be overwritten:" << std::endl
<< path.toStdString() << std::endl
<< "Are you sure? " << (multiple ? "(Yes/YesAll/No/NoAll)" : "(Yes/No)") << std::endl;
std::string answer;
std::cin >> answer;
qanswer = QString::fromStdString(answer).toLower();
std::cin.clear();
}
当输入"Yes"、"No"、"YesAll"或"NoAll"(不区分大小写(时,程序会按预期继续,但当用户输入其他内容时,cmd会抛出以下内容:
'[input]'未被识别为内部或外部命令[…]
然后再次显示"C:\path\to\exe>",用户可以继续输入,直到键入正确的值之一。一旦输入了有效的字符串,它就会按预期再次继续。
我试过这个答案和std::getline()
一样,但没有区别。
那么,如何防止出现错误并继续显示cout
呢?
AttachConsole
只是连接到父进程的控制台,它不会阻止父进程也从中读取。因此,控制台输入在父进程(cmd.exe
(和您的应用程序之间交错,管理起来可能会有问题(有些人建议杀死父进程,这显然不是一个好主意(。
您可以做的是始终创建一个新的控制台(请参阅AllocConsole
(。
或者,如果您想重复使用相同的控制台,可以将控制台子系统作为目标(链接器选项/SUBSYSTEM:CONSOLE
(,并使用常规的main()
函数而不是WinMain
(是的,您可以在main()
内创建Win32窗口和来处理控制台I/O(。
您甚至可以拥有一个多子系统源,该源可以链接为Windows以及控制台子系统,并使用这样的填充程序(nCmdShow
和命令行参数仍有待实现(:
HWND hwnd;
int main() {
std::thread t([] {
// let GUI run in its own thread ...
WinMain(GetModuleHandle(NULL), NULL, "", SW_SHOWDEFAULT);
exit(0);
});
// meanwhile in this thread we handle console I/O ...
std::string s;
std::cout << "Press Enter to exit" << std::endl;
while (std::getline(std::cin, s)) {
if (s == "")
break;
std::cout << "Hello " << s << std::endl;
}
PostMessageA(hwnd, WM_CLOSE, 0, 0);
t.join();
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Your normal WinMain.
// CreateWindow, GetMessage loop etc. . .
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- "input.length();"的目的是什么?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- Is !NaN not a NaN?
- 如何使用"derived input class"创建派生类?
- Directx 11 - CompileFromFile() is not compiling
- Centos7 g++ "to_string is not in a member of std"
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- Where is std::hardware_destructive_interference_size?
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- What is unordered_set in C++
- 你如何理解"std: :forward is just syntactic sugar"?这是真的吗?
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- C++ is calculating wrong
- Is it good to use SDL_PIXELFORMAT_UNKNOWN?
- reference_wrapper导致"incomplete type is not allowed"
- 为什么即使在定义之后仍存在"Identifier is undefined error "?
- libxml2 在 C++, 设置解析器的编码 - "Input is not proper UTF-8"