【问题标题】:popen does not trap all the outputs of a commandpopen 不会捕获命令的所有输出
【发布时间】:2015-12-16 07:22:28
【问题描述】:
#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();
}

使用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

如您所见,使用make 命令,输出被qDebug 捕获并显示。 但是,ping 的情况并非如此

无论是错误还是什么,我都希望通过我的程序通过 qDebug 捕获和显示每个输出。

我现在该怎么办?

【问题讨论】:

    标签: c linux qt popen


    【解决方案1】:

    在您的代码中:

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

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

    FILE* file1 = popen ("make 2>&1", "r");
    

    进一步阅读:

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

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2017-02-26
      • 2013-01-16
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      相关资源
      最近更新 更多