1
我寫在Qt的程序和當前使用POPEN運行Linux命令和讀取輸出到一個字符串:如何獲取使用QProcess調用的程序的返回stdout?
QString Test::popenCmd(const QString command) {
FILE *filePointer;
int status;
int maxLength = 1024;
char resultStringBuffer[maxLength];
QString resultString = "";
filePointer = popen(command.toStdString().c_str(), "r");
if (filePointer == NULL) {
return 0;
}
while (fgets(resultStringBuffer, maxLength, filePointer) != NULL) {
resultString += resultStringBuffer;
}
status = pclose(filePointer);
if (status == 0) {
return resultString;
} else {
return 0;
}
}
所以我想扔上面的代碼作爲離我更願意如果可能,請使用Qt提供的更高級別的設施。有沒有人有過如何用QProcess做這個事情的例子,或者至少有一個關於如何完成的粗略想法?
這是值得的,它將在Linux上運行。
謝謝
[讀取QProcess輸出到字符串]的可能重複(http://stackoverflow.com/questions/17344807/read-qprocess-output-to-string) –