popen不会捕获命令的所有输出

Aqu*_*irl -1 c linux qt popen

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QProcess>
#include <QFile>
#include <QDebug>
#include <stdio.h>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    FILE* file1 = popen ("make", "r");

    char buff[5122];

    while(fgets(buff, sizeof(buff), file1)!=NULL)
    {
        qDebug() << "from here: " << buff;
    }


    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral ("qrc:/main.qml")));
    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

使用make命令输出是:

QML debugging is enabled. Only use this in a safe environment.
from here: make: Nothing to be done forfirst'.`

使用ping命令输出是:

QML debugging is enabled. Only use this in a safe environment.
Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
            [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
            [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
            [-w deadline] [-W timeout] [hop1 ...] destination
Run Code Online (Sandbox Code Playgroud)

如您所见,使用make命令输出被捕获并显示qDebug.但是,事实并非如此ping.

无论是错误还是其他什么,我希望通过我的程序通过qDebug捕获并显示每个输出.

我现在该怎么办?

Tho*_*key 5

在你的代码中:

FILE* file1 = popen ("make", "r");
Run Code Online (Sandbox Code Playgroud)

你可以使用任何shell命令在机器上有用.例如,您可以将标准错误重定向到与标准输出相同的目标,并通过管道捕获两者:

FILE* file1 = popen ("make 2>&1", "r");
Run Code Online (Sandbox Code Playgroud)

进一步阅读:

虽然技术上可以打开多个管道到子进程,但它比单行调用复杂得多popen: