Reading file as written by external process
Solved
General and Desktop
-
wrote on 22 Apr 2021, 16:29 last edited by
I have a log file that is being written asynchronously from a docker command using a tee. I want to either copy and/or parse the file up to the current point it is written. Is this possible with QFile or at all? Any advice is appreciated.
-
What wrong with simply open the file and read it?
-
Hi and welcome to the forums
Yes it will work.
You have to use a timer or similar to read it from time to time
Disclaimer. fast code. needs more love to be solid.class Reader : QObject { QFile file; QTextStream in; QTimer timer; public: explicit Reader(QObject *parent) : QObject(parent) { connect(&timer, &QTimer::timeout, this, [this]() { while (!in.atEnd()) { QString line = in.readLine(); qDebug() << line; } }); } void Process(QString filename) { file.setFileName( filename); in.setDevice(&file); if ( ! file.open(QFile::ReadOnly)) { qDebug() << "open error" << file.errorString(); return; } timer.start(1000); qDebug() << "started"; } };
using
Reader *LogReader = new Reader(this); LogReader->Process("e:/test.txt");
Note file must exist when you try.
-
wrote on 29 Apr 2021, 05:23 last edited by
Got it working, thank you. Errors were unrelated to the program writing the file.
1/4