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. QDataStream write file size to file header ?

QDataStream write file size to file header ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 1.2k Views
  • 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
    sonichy
    wrote on 6 May 2018, 04:12 last edited by
    #1

    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
    1
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 6 May 2018, 05:26 last edited by
      #2

      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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      7
      • V Offline
        V Offline
        VRonin
        wrote on 8 May 2018, 07:59 last edited by
        #3

        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
        3
        • G Offline
          G Offline
          Gojir4
          wrote on 8 May 2018, 08:10 last edited by
          #4

          Maybe you are looking for QTextStream ?

          1 Reply Last reply
          1

          1/4

          6 May 2018, 04:12

          • Login

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