我有一個應用程序(我沒有源代碼),它的輸出打印到控制檯。我正在將其輸出寫入文件中;如何閱讀使用Qt更新文本文件?
./otherApp > out.txt
並與C++ Qt編程,我寫一個應用程序,它也應該閱讀它的輸出同時。但是,我的代碼只是讀出out.txt文件創建。我的意思是,當otherApp繼續寫輸出時,如果我的項目讀完了,它不會讀取輸出文件的其餘部分。
這裏是QTextStream有關讀取輸出的常用方法。
QString fileName = "/.1/Work/appOutput/out1";
QFile inputFile(fileName);
if(inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while(!in.atEnd())
{
QString line = in.readLine();
qDebug() << line;
}
inputFile.close();
}
我的問題是我怎麼可以讀出文本文件,直到我關閉otherApp或之後,在沒有裏面寫什麼?
編輯:
感謝@Bowdzone我改變了我的代碼,它工作正常。這是我的代碼。
QString program = "/.1/Work/otherApp";
QStringList arguments;
arguments << "-x" << "1002";
QProcess *myProcess = new QProcess();
myProcess->start(program, arguments);
QString strOut = myProcess->readAllStandardOutput();
qDebug() << QTime::currentTime().toString() << strOut;
myProcess->waitForFinished();
但是,只要有登錄應用程序,qDebug()就不會顯示otherApp的輸出。看起來像myProcess->waitForFinished()
不應該放置,因爲一旦應用程序啓動它創建另一個進程,我不能看到Qt項目中的日誌。你能幫我解釋我怎麼能在同一時間看到日誌嗎?
您可以使用[QProcess](http://doc.qt.io/qt-5/qprocess.html)從應用程序中啓動其他應用程序,並使用其方法直接讀取輸出,而不必使用一個文本文件。 – Bowdzone
@Bowdzone你可以給我小樣更清楚嗎? – goGud
這一切都在文檔中。 [Here](http://doc.qt.io/qt-5/qprocess.html#details)它展示瞭如何使用'QProcess'啓動一個外部程序。然後,只要程序寫了一些標準輸出到你可以使用['readAllStandardOutput()'讀取]的''readyReadStandardOutput()'](http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput) '](http://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput) – Bowdzone