How to get the output of system() command

2019-06-05 23:26发布

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 !

2条回答
劳资没心,怎么记你
2楼-- · 2019-06-05 23:52
QProcess process;
process.start("cmd /c dir C:\\");
process.waitForFinished(-1);
QByteArray out = process.readAllStandardOutput();
查看更多
迷人小祖宗
3楼-- · 2019-06-05 23:59

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);
查看更多
登录 后发表回答