Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Best way to write lines to a files over time?

Best way to write lines to a files over time?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 495 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Smeeth
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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)

      S 1 Reply Last reply
      2
      • mrjjM mrjj

        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)

        S Offline
        S Offline
        Smeeth
        wrote on last edited by
        #3

        @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!

        mrjjM 1 Reply Last reply
        0
        • S Smeeth

          @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!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved