C++ popen() 将执行的命令的输出写入 cout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44610978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:37:14  来源:igfitidea点击:

popen() writes output of command executed to cout

c++filepopen

提问by John Smith

I am writing an application that needs to open another process and get it's output. Online everywhere I read I have to use popenand read from the file.

我正在编写一个需要打开另一个进程并获取它的输出的应用程序。在线阅读我必须使用popen并从文件中读取的任何地方。

But I can't read from it. The output of the command gets output into the console window of the calling application. Below is the code I am using. I added some prints to debug.

但我无法从中读取。命令的输出将输出到调用应用程序的控制台窗口中。下面是我正在使用的代码。我添加了一些打印来调试。

#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <array>

int main()
{
    // some command that fails to execute properly.
    std::string command("ls afskfksakfafkas");

    std::array<char, 128> buffer;
    std::string result;

    std::cout << "Opening reading pipe" << std::endl;
    FILE* pipe = popen(command.c_str(), "r");
    if (!pipe)
    {
        std::cerr << "Couldn't start command." << std::endl;
        return 0;
    }
    while (fgets(buffer.data(), 128, pipe) != NULL) {
        std::cout << "Reading..." << std::endl;
        result += buffer.data();
    }
    auto returnCode = pclose(pipe);

    std::cout << result << std::endl;
    std::cout << returnCode << std::endl;

    return 0;
}

Reading is never actually printed to the my coutand result is an empty string. I clearly see the output of the command in my terminal. If the command exits gracefully the behaviour is as expected. But I only capture the output for error cases.

Reading 从未真正打印到 mycout并且结果是一个空字符串。我在终端中清楚地看到了命令的输出。如果命令正常退出,则行为符合预期。但我只捕获错误情况的输出。

回答by John Smith

Popen doesn't capture stderr only stdout. Redirecting stderr to stdout fixes the issue.

Popen 不只捕获 stderr 的 stdout。将 stderr 重定向到 stdout 可以解决此问题。

#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <array>

int main()
{
    std::string command("ls afskfksakfafkas 2>&1");

    std::array<char, 128> buffer;
    std::string result;

    std::cout << "Opening reading pipe" << std::endl;
    FILE* pipe = popen(command.c_str(), "r");
    if (!pipe)
    {
        std::cerr << "Couldn't start command." << std::endl;
        return 0;
    }
    while (fgets(buffer.data(), 128, pipe) != NULL) {
        std::cout << "Reading..." << std::endl;
        result += buffer.data();
    }
    auto returnCode = pclose(pipe);

    std::cout << result << std::endl;
    std::cout << returnCode << std::endl;

    return 0;
}

回答by Антон М

You have to add "2>&1" at the end of command string

您必须在命令字符串的末尾添加“2>&1”

command.append(" 2>&1");

there is a full example https://www.jeremymorgan.com/tutorials/c-programming/how-to-capture-the-output-of-a-linux-command-in-c/

有一个完整的例子https://www.jeremymorgan.com/tutorials/c-programming/how-to-capture-the-output-of-a-linux-command-in-c/