Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved QDataStream write file size to file header ?

    General and Desktop
    4
    4
    813
    Loading More Posts
    • 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.
    • sonichy
      sonichy last edited by

      QDataStream write 4 more bytes to file header which seems like file size, and causes file error, file.write() will not.

      QFile file(path);
      if (file.open(QFile::WriteOnly)) {
          QDataStream DS(&file);
          QByteArray BA = "aaaaaaaaaaa";
          DS << BA;
          file.close();
      }
      
      QFile file(path);
      if (file.open(QFile::WriteOnly)) {    
          QByteArray BA = "aaaaaaaaaaa";
          file.write(BA);
          file.close();
      }
      

      https://github.com/sonichy

      1 Reply Last reply Reply Quote 1
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        And what's the actual problem? QDataStream is an internal stream format which allows you to write and read Qt data types directly to a file. Therefore it need to add some information to know which data type is there and others like e.g. it's content size.

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 7
        • VRonin
          VRonin last edited by

          those 4 bytes are the lenght of the array so that you can read it back otherwise you wouldn't know where to stop...

          Source: https://code.woboq.org/qt5/qtbase/src/corelib/serialization/qdatastream.cpp.html#_ZN11QDataStream10writeBytesEPKcj

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply Reply Quote 3
          • Gojir4
            Gojir4 last edited by

            Maybe you are looking for QTextStream ?

            1 Reply Last reply Reply Quote 1
            • First post
              Last post