Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Best way to write lines to a files over time?



  • I am creating a class MyFileWriter that has a slot called addData that writes some data to a file every 20 seconds or so. My class as it is does not work, I am not sure why. The intended file is created, but nothing gets written to it and the file is blank. I am sure the slot is getting called and the FileData object has the expected values.

    Is there a better way to do this?

    Here is my class:

    #include "myfilewriter.h"
    #include "mydata.h"
    #include <QFile>
    
    MyFileWriter::MyFileWriter(QFileInfo fileInfo) :
        myFile(new QFile(fileInfo.absoluteFilePath()))
    {
    }
    
    MyFileWriter::~MyFileWriter(){
        delete myFile;
    }
    
    void MyFileWriter::addData(FileData data){
    
        if (myFile->open(QFile::WriteOnly)){
            QTextStream writeStream(myFile);
            writeStream << data.getTime();
            writeStream << ",";
            writeStream << data.getValue1();
            writeStream << ",";
            writeStream << data.getValue2();
            writeStream << ",";
            writeStream << data.getValue3();
            writeStream << ",";
            writeStream << "\n";
        }
    }
    

    And here is the header, if that is necessary:

    #include "filedata.h"
    #include <QFileInfo>
    #include <QTextStream>
    
    class MyFileWriter: public QObject
    {
        Q_OBJECT
    public:
        MyFileWriter(QFileInfo fileInfo);
        ~MyFileWriter();
    
    public slots:
        void addData(FileData data);
    
    private:
        QFile *myFile;
    
    
    };
    

    Is it clear what is wrong with my code? The slot is getting called and the FileData object is correct, but nothing gets written to the file.

    And I imagine there must be a more efficient way of writing to a file without creating a new QTextStream everytime?


  • Lifetime Qt Champion

    Hi
    From first look, code looks fine.
    However, if nothing is written, i wonder if
    it enters
    if (myFile->open(QFile::WriteOnly)){
    QTextStream writeStream(myFile);
    ....

    else its a case of MyFileWriter not being deleted and it never closes the QFile.
    ( when QFiles is deleted, it will call close() automatically and flush the data)
    You can add
    myFile->flush() to
    MyFileWriter::addData ( as last statement )
    to have it commit data and still be open.

    you do not need to new it every time. You can create it once
    and write to it and then flush and first call
    close() when finished. ( or indirectly by delete MyFileWriter instance)



  • @mrjj You are correct, I just had to add a call to the QFile.close() and that fixed everything. I guess that also means I am not deleting the FileWriter properly, so I will look into that as well.

    The very short examples of the QTextStream page don't show any call to QFile.close(), so I wasn't sure what I had to do to close the stream.

    Thank you!


  • Lifetime Qt Champion

    @Smeeth
    Hi. super.
    well the sample relies on the QFile running out of scope and the destructor calls close.
    If you need to write to it over time, its perfectly good to only call flush and
    first close() at app shutdown.
    If you close it and only open it with QFile::WriteOnly it will overwrite old file content.
    However, you can open it with QFile::append and it will add to file.


Log in to reply