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. QFile. Only close() do flush()

QFile. Only close() do flush()

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 609 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.
  • D Offline
    D Offline
    DungeonLords
    wrote on last edited by DungeonLords
    #1

    I use QFile. After I finish writing I do flush() and then I want readAll() file back. Why zero inside?

       QFile file;
       file.setFileName("test.bin");
       if(!file.open(QIODeviceBase::ReadWrite | QIODeviceBase::Truncate)){
           qWarning() << "err Can't open file" << file.fileName();
       }
       const quint8 myData[] = { 1,2,3 };
       file.write((const char*)myData, sizeof(myData));
       file.flush();
    
    //    //Why to get true arr size I need close, open?
    //    csvDefaults.close();
    //    csvDefaults.open(QIODeviceBase::ReadWrite);
    
       const QByteArray arr(file.readAll());
       qDebug() << "arr size=" << arr.size();
       file.close();
    

    I can fix my problem if I do close() and open(), but why I can't do flush() only?

    Full code here.

    JonBJ M 2 Replies Last reply
    0
    • D DungeonLords

      I use QFile. After I finish writing I do flush() and then I want readAll() file back. Why zero inside?

         QFile file;
         file.setFileName("test.bin");
         if(!file.open(QIODeviceBase::ReadWrite | QIODeviceBase::Truncate)){
             qWarning() << "err Can't open file" << file.fileName();
         }
         const quint8 myData[] = { 1,2,3 };
         file.write((const char*)myData, sizeof(myData));
         file.flush();
      
      //    //Why to get true arr size I need close, open?
      //    csvDefaults.close();
      //    csvDefaults.open(QIODeviceBase::ReadWrite);
      
         const QByteArray arr(file.readAll());
         qDebug() << "arr size=" << arr.size();
         file.close();
      

      I can fix my problem if I do close() and open(), but why I can't do flush() only?

      Full code here.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @DungeonLords
      This may or may not apply to QFile, I don't know, but if you open a file for read/write, write to it, then:

      • After write file pointer is at end of written bytes, need to seek to beginning/rewind to read from start; and
      • Many C/C++ implementations require (or at least used to) seek between write/read or read/write anyway.

      And btw QFile::readAll() says "Reads all remaining data from the device", that does not mean all data in file from start, it means from file pointer onward only. As per first point above.

      1 Reply Last reply
      2
      • D DungeonLords

        I use QFile. After I finish writing I do flush() and then I want readAll() file back. Why zero inside?

           QFile file;
           file.setFileName("test.bin");
           if(!file.open(QIODeviceBase::ReadWrite | QIODeviceBase::Truncate)){
               qWarning() << "err Can't open file" << file.fileName();
           }
           const quint8 myData[] = { 1,2,3 };
           file.write((const char*)myData, sizeof(myData));
           file.flush();
        
        //    //Why to get true arr size I need close, open?
        //    csvDefaults.close();
        //    csvDefaults.open(QIODeviceBase::ReadWrite);
        
           const QByteArray arr(file.readAll());
           qDebug() << "arr size=" << arr.size();
           file.close();
        

        I can fix my problem if I do close() and open(), but why I can't do flush() only?

        Full code here.

        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        @DungeonLords
        As @JonB said, you need to reposition to the beginning of the file.

        You can use:
        file.seek(0);
        or
        file.reset();

        1 Reply Last reply
        2
        • D DungeonLords has marked this topic as solved on

        • Login

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