popen 不会捕获命令的所有输出
popen does not trap all the outputs of a command
#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 for
第一个'。`
使用 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 捕获和显示每个输出。
我现在该怎么办?
在您的代码中:
FILE* file1 = popen ("make", "r");
您可以使用任何在机器上有用的 shell 命令。例如,您可以将 标准错误 重定向到与 标准输出 相同的目的地,并捕获 both 通过管道:
FILE* file1 = popen ("make 2>&1", "r");
进一步阅读:
- c popen won't catch stderr
- how to control popen stdin, stdout, stderr redirection?
虽然在技术上可以打开多个管道到一个子进程,但它比单行调用 popen
:
复杂得多
- Does this multiple pipes code in C makes sense?
- How do I use two pipes in Unix C?
- Multiple pipe implementation using system call fork() execvp() wait() pipe() - it is simply not working
#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 for
第一个'。`
使用 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 捕获和显示每个输出。
我现在该怎么办?
在您的代码中:
FILE* file1 = popen ("make", "r");
您可以使用任何在机器上有用的 shell 命令。例如,您可以将 标准错误 重定向到与 标准输出 相同的目的地,并捕获 both 通过管道:
FILE* file1 = popen ("make 2>&1", "r");
进一步阅读:
- c popen won't catch stderr
- how to control popen stdin, stdout, stderr redirection?
虽然在技术上可以打开多个管道到一个子进程,但它比单行调用 popen
:
- Does this multiple pipes code in C makes sense?
- How do I use two pipes in Unix C?
- Multiple pipe implementation using system call fork() execvp() wait() pipe() - it is simply not working