简体   繁体   English

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

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

I have the following code: 我有以下代码:

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);

This works to get the standard output and exit code of the executed command; 这样可以获取已执行命令的标准输出和退出代码。

When I run this code with an invalid command though (something like "dfakjhfasidufha" ), an error message appears in my console: 但是,当我使用无效命令运行此代码时(类似于"dfakjhfasidufha" ),控制台中会显示一条错误消息:

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

How can I suppress this message? 如何隐藏此消息?

The message comes from windows (I suppose) so it is default mode to yield to user when something is wrong. 该消息来自Windows(我想),因此这是默认模式,当出现问题时会向用户屈服。 Buy you can get rid of this by redirecting to nul. 购买,您可以通过重定向到nul摆脱此情况。 something like yourcommand > nul 2> nul 类似于yourcommand> nul 2> nul

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM