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 question
Forum Updated to NodeBB v4.3 + New Features

QDataStream question

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 4.5k Views 2 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.
  • RIVOPICOR Offline
    RIVOPICOR Offline
    RIVOPICO
    wrote on last edited by
    #3

    yeah i have different files and one qbytearray to get these files (extract them). For example i create one qbytearray like:

    QByteArray trama(1024,'0');
    

    Now i know the size of my trama so i can seek this trama and with this trama i know the sizes of my files to extract them. But if my qbytearray need more space i dont know how i will read my qbytearray if i dont know the size. So i can't seek my trama so what you recommend me? more or less.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #4

      You need to save the size in the file along with the actual data. After that you can first read the size and then read that specific number of bytes into a byte array back again.

      1 Reply Last reply
      1
      • RIVOPICOR Offline
        RIVOPICOR Offline
        RIVOPICO
        wrote on last edited by
        #5

        For reading the size i must to use qdatastream i think other way is not possible. But i'm not sure what does qdatastream add me the binary data at the first of my file and then read it¿?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #6

          Here's an example. Hope it works :-)

          #include <QGuiApplication>
          #include <QDataStream>
          #include <QDebug>
          #include <QFile>
          
          void write_bytearray_to_file()
          {
              const QByteArray ba("Hello world");
              QFile file("file.bin");
              file.open(QIODevice::WriteOnly);
              QDataStream out(&file);
              out << ba.size();
              out.writeRawData( ba.constData(), ba.size() );
          }
          
          void read_bytearray_from_file()
          {
              QFile file("file.bin");
              file.open(QIODevice::ReadOnly);
              QDataStream in(&file);
              int ba_size = 0;
              QByteArray ba;
              in >> ba_size;
              ba.resize(ba_size);
              in.readRawData(ba.data(), ba_size);
              qDebug() << ba_size;
              qDebug() << ba;
          }
          
          int main(int argc, char *argv[])
          {
              QGuiApplication app(argc, argv);
              write_bytearray_to_file();
              read_bytearray_from_file();
              return EXIT_SUCCESS;
          }
          
          1 Reply Last reply
          3
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #7

            Here is a slightly different version, using writeBytes and readBytes:

            void write_bytearray_to_file()
            {
                const QByteArray ba("Hello world");
                QFile file("file.bin");
                file.open(QIODevice::WriteOnly);
                QDataStream out(&file);
                out.writeBytes( ba.constData(), ba.size() );
            }
            
            void read_bytearray_from_file()
            {
                QFile file("file.bin");
                file.open(QIODevice::ReadOnly);
                QDataStream in(&file);
                uint ba_size = 0;
                char *c = nullptr;
                in.readBytes(c, ba_size);
                QByteArray ba(c, ba_size);
                delete [] c;
                qDebug() << ba_size;
                qDebug() << ba;
            }
            
            1 Reply Last reply
            1
            • RIVOPICOR Offline
              RIVOPICOR Offline
              RIVOPICO
              wrote on last edited by
              #8

              it's frequent this error?

              1297780736
              QIODevice::read (QFile, "\Desktop\visual\build-binder-Qt_5_7_0_msvc2
              013_static-Debug\debug\sha.exe"): maxSize argument exceeds QByteArray size limit
              
              ASSERT failure in QList<T>::operator[]: "index out of range", file C:\Qt\Qt5.7.0
              \5.7\mingw53_32\include/QtCore/qlist.h, line 545
              
              This application has requested the Runtime to terminate it in an unusual way.
              Please contact the application's support team for more information.
              

              I was trying like you teach me write the size and write data. And then read this size and extract my data.

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

                Hi
                You should qDebug() the values when you write them and when you read them in again.

                It sounds like you read something wrong and use for size
                "maxSize argument exceeds QByteArray size limit"

                RIVOPICOR 1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  You should qDebug() the values when you write them and when you read them in again.

                  It sounds like you read something wrong and use for size
                  "maxSize argument exceeds QByteArray size limit"

                  RIVOPICOR Offline
                  RIVOPICOR Offline
                  RIVOPICO
                  wrote on last edited by RIVOPICO
                  #10

                  @mrjj i think i cant include binary data in pe format not sure to be read. I could use other file to get my trama but i dont know if it is good idea i think not because i will write my trama in this file and to read my trama again read the other file to get my trama or size of my trama. I'm saying like external file. But i think it's very ugly to do that. I could delimiter my trama and extract it but i think is difficult to do. Anyways, i dont know what is the maximum length of one qstring ¿? not sure.

                  ? 1 Reply Last reply
                  0
                  • RIVOPICOR RIVOPICO

                    @mrjj i think i cant include binary data in pe format not sure to be read. I could use other file to get my trama but i dont know if it is good idea i think not because i will write my trama in this file and to read my trama again read the other file to get my trama or size of my trama. I'm saying like external file. But i think it's very ugly to do that. I could delimiter my trama and extract it but i think is difficult to do. Anyways, i dont know what is the maximum length of one qstring ¿? not sure.

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #11

                    @RIVOPICO Max length is 2 GB

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

                      Ps. If you reading PE exe file, you should NOT ever put the input in a string.
                      It must be bytearray at all times if ever to be an exe file again. :)

                      1 Reply Last reply
                      0
                      • RIVOPICOR Offline
                        RIVOPICOR Offline
                        RIVOPICO
                        wrote on last edited by
                        #13

                        yeah i can add qbytearray but for reading it? i must to know the size. or use bin or .dat or other file.

                        mrjjM 1 Reply Last reply
                        0
                        • RIVOPICOR RIVOPICO

                          yeah i can add qbytearray but for reading it? i must to know the size. or use bin or .dat or other file.

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

                          @RIVOPICO
                          Hi
                          To know size later. u must store it yourself.
                          Size|bytearray
                          Then you can know when reading it again.

                          1 Reply Last reply
                          0

                          • Login

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