Using QDataStream with an updating file

  • I am writing a tool that will parse a binary recording file real-time. I was planning on using QDataStream for this purpose. The intention is to open the file and initially parse as much of the file as is currently available, and then set a timer, to periodically read whatever was recorded in the interim. My question is, if I leave data in the stream and the file is written to in the meantime, will the QDataStream update to reflect the current state of the file, in particular bytesAvailable when I next query it?

  • Depends if the QIODevice does that, if you are using QFile it should be fine

  • @VRonin yes, the QIODevice is a QFile

  • #include <QApplication>
    #include <QTimer>
    #include <QFile>
    #include <QDataStream>
    #include <QTime>
    #include <QDebug>
    int main(int argc, char* argv[])
        QApplication a(argc, argv);
        QFile readFile("test.dat");
        QFile writeFile("test.dat");
        if (!
        if (!
        QDataStream readStream(&readFile);
        QDataStream writeStream(&writeFile);
        QTimer writeTimer;
        QTimer readTimer;
        QObject::connect(&writeTimer, &QTimer::timeout, [&writeStream, &readTimer]()->void {
            const QTime currTime = QTime::currentTime();
            qDebug() << "Saving: " << currTime;
            writeStream << currTime;
            qDebug() << "bytesAvailable: " << writeStream.device()->bytesAvailable();
        QObject::connect(&readTimer, &QTimer::timeout, [&readStream]()->void {
            qDebug() << "bytesAvailable: " << readStream.device()->bytesAvailable();
            QTime currTime;
            readStream >> currTime;
            qDebug() << "Loading: " << currTime;
        QTimer::singleShot(0, &writeTimer, SLOT(start()));
        QTimer::singleShot(10000, &writeTimer, SLOT(stop()));
        return a.exec();

  • @VRonin Thanks... that's exactly what I was hoping for!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.