简体   繁体   English

如何获取system()命令Qt C ++的输出?

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

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 ! 谢谢 !

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

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() . 您可以将标准输出路径修改为您要读取的管道,但是使用popen()而不是system()会更容易。

Since you appear to be using Windows, you would use _popen() . 由于您似乎正在使用Windows,因此应使用_popen()

#include <stdio.h>

....

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM