How to get the output of system() command qt c++?

China☆狼群 提交于 2019-12-08 07:31:00

问题


I want to get the output of this command for example :

system("dir C:\");

or of :

QProcess::execute("cmd /c dir C:\");

How to do that ?

Thanks !


回答1:


QProcess process;
process.start("cmd /c dir C:\\");
process.waitForFinished(-1);
QByteArray out = process.readAllStandardOutput();



回答2:


You can modify the standard output path to be a pipe which you read from, but it would be easier to use popen() instead of system().

Since you appear to be using Windows, you would use _popen().

#include <stdio.h>

....

FILE *fp = _popen("dir c:\", "r");
....
while (!feof(fp)) {
    ....
}
fclose(fp);


来源:https://stackoverflow.com/questions/12696924/how-to-get-the-output-of-system-command-qt-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!