I am writing a program in Qt and currently using popen to run a linux command and read the output into a string:
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;
}
}
So I'd like to throw the above code away as I'd prefer to use higher level facilities provided by Qt if possible. Does anyone have an example of how to do this with QProcess or atleast a rough idea of how it could be done?
For what it's worth, this will be run on Linux.
Thank you