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. How to prepend the data size to a file
Forum Updated to NodeBB v4.3 + New Features

How to prepend the data size to a file

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 861 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
    summit
    wrote on last edited by
    #1

    I am saving the QAbstract tree model to a dat file.

    How can i prepend the size of total bytes to be written to the start of dat file.

    void saveTreeStructureToFile(const QModelIndexList &indexes , const std::string stdstrFilePath)
    {
        QMimeData *mimeData = new QMimeData;
        QByteArray data; //a kind of RAW format for datas
        QDataStream stream(&data, QIODevice::WriteOnly);
        QList<TreeItem *> nodes;
        foreach(const QModelIndex &index, indexes) {
            TreeItem *node = getItem(index);
            if (!nodes.contains(node))
                nodes << node;
        }
        stream << nodes.count();
        foreach(TreeItem *node, nodes) {
            buildTree(node, stream);
        }
        mimeData->setData(s_treeNodeMimeType, data);
        std::string st = stdstrFilePath.substr(0, stdstrFilePath.size() - 3);
        st.append("dat");
        const QString path = st.c_str();
        QFile file(path);
        file.open(QIODevice::WriteOnly);
        QDataStream out(&file);
        out << *mimeData;
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • S summit

      I am saving the QAbstract tree model to a dat file.

      How can i prepend the size of total bytes to be written to the start of dat file.

      void saveTreeStructureToFile(const QModelIndexList &indexes , const std::string stdstrFilePath)
      {
          QMimeData *mimeData = new QMimeData;
          QByteArray data; //a kind of RAW format for datas
          QDataStream stream(&data, QIODevice::WriteOnly);
          QList<TreeItem *> nodes;
          foreach(const QModelIndex &index, indexes) {
              TreeItem *node = getItem(index);
              if (!nodes.contains(node))
                  nodes << node;
          }
          stream << nodes.count();
          foreach(TreeItem *node, nodes) {
              buildTree(node, stream);
          }
          mimeData->setData(s_treeNodeMimeType, data);
          std::string st = stdstrFilePath.substr(0, stdstrFilePath.size() - 3);
          st.append("dat");
          const QString path = st.c_str();
          QFile file(path);
          file.open(QIODevice::WriteOnly);
          QDataStream out(&file);
          out << *mimeData;
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @summit
      you have 2 possibilities,

      either calculate the size beforehand and write it first or write 0 in the first few bytes (4?) and overwrite them at the end.

      I you open your file in ReadWrite you can overwrite partially without loosing the rest. Just make sure to only change the reserved bytes


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        summit
        wrote on last edited by
        #3

        @J-Hilk Thank you very much , i have a question if i write 0 in the beginning how do i overwrite it with the size at the end.

        Can you please show me the code .

        suppose if i write 0 to the file

        stream << 0 // write 0 to the file and than write all other data
        stream << nodes.count();
        

        now how do i overwrite this data in the end

        J.HilkJ 1 Reply Last reply
        0
        • S summit

          @J-Hilk Thank you very much , i have a question if i write 0 in the beginning how do i overwrite it with the size at the end.

          Can you please show me the code .

          suppose if i write 0 to the file

          stream << 0 // write 0 to the file and than write all other data
          stream << nodes.count();
          

          now how do i overwrite this data in the end

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @summit actually looking at your code, the following should work

          QFile file(path);
              file.open(QIODevice::WriteOnly);
              QDataStream out(&file);
              out << static_cast<qint64 >(0) << *mimeData;
          
            file.close();
            if(file.open(QIODevice::ReadWrite)
                file.write(static_cast<qint64 >(file.size()));
            file.close();
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          4
          • S Offline
            S Offline
            summit
            wrote on last edited by
            #5

            @J-Hilk said in How to prepend the data size to a file:

            static_cast<qint64 >(0)

            @J-Hilk Thank you very much , i am not able to prepend the size of the bytes.

            J.HilkJ JonBJ S 3 Replies Last reply
            0
            • S summit

              @J-Hilk said in How to prepend the data size to a file:

              static_cast<qint64 >(0)

              @J-Hilk Thank you very much , i am not able to prepend the size of the bytes.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @summit you couldn't be more vague, if you tried to.

              What error do you have?
              The code os untested from the top of my head, just fixed a typo there may be more


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              4
              • S summit

                @J-Hilk said in How to prepend the data size to a file:

                static_cast<qint64 >(0)

                @J-Hilk Thank you very much , i am not able to prepend the size of the bytes.

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

                @summit
                @J-Hilk 's code looks good to me. You can also save closing and re-opening the file by changing the code that does that when finished writing your data to:

                file.flush();
                file.seek(0);
                file.write(static_cast<qint64 >(file.size()));
                file.close();
                
                1 Reply Last reply
                2
                • S summit

                  @J-Hilk said in How to prepend the data size to a file:

                  static_cast<qint64 >(0)

                  @J-Hilk Thank you very much , i am not able to prepend the size of the bytes.

                  S Offline
                  S Offline
                  summit
                  wrote on last edited by
                  #8

                  @J-Hilk sorry i wanted to write that i am able to prepend the file now and my problem is solved.

                  unfortunately it was a type error from my side in my previous comment.

                  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