如何在 C++ 中捕获通过 _popen 启动的命令的所有输出?

How do I capture all output of a command launched through _popen, in C++?

我有以下代码:

FILE* pipe = _popen(command_.c_str(), "r");
if (!pipe)
{
    throw std::runtime_error{
        "Subprocess::Run: error starting [" + command_ + "]"
    };
}

std::array<char, 512> buffer;
while (!feof(pipe))
    if (fgets(buffer.data(), buffer.size(), pipe) != NULL)
        output_.append(buffer.data());

exit_code_ = _pclose(pipe);

这可以获取执行命令的标准输出和退出代码;

当我 运行 此代码使用无效命令时(类似于 "dfakjhfasidufha"),我的控制台中出现一条错误消息:

'dfakjhfasidufha' is not recognized as an internal or external command, 
operable program or batch file.

我怎样才能禁止这条消息?

消息来自windows(我想)所以当出现问题时默认模式让步给用户。 Buy 你可以通过重定向到 nul 来摆脱它。类似于 yourcommand > nul 2> nul