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. Read binary file by block
Forum Updated to NodeBB v4.3 + New Features

Read binary file by block

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 2.5k 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.
  • C Offline
    C Offline
    cerbica
    wrote on last edited by
    #1

    Hi,
    I'm new of Qt. I need to perform the following steps:

    1. Read a binary file (extension unknown) by block of fixed dimension.
    2. Append each block to a QStringList object (cannot change object type) in order to send it to a remote server
    3. On the remote server (wrote in C code) convert back the binary file without information lost.

    I perform the following step:

    QVector<char*> vect;
    if (! m_fi.open(QIODevice::ReadOnly)){
    return;
    }
    while(!m_fi.atEnd())
    {
    vect[idx] = new char[BLOCK_DIMENSION];
    m_fi.read(vect[idx],BLOCK_DIMENSION);
    }

    QStringList parameters;
    parameters.append(vect[idx]);
    /* Send "parameters" to remote server */

    (On the remore C server)
    How I can return back to the original binary file?

    Many thanks
    Sarah

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

      Hi and welcome to the forums
      Are you sure QString will handle the binary data well ?
      Normally i use QByteArray as not to get into issues with NULL values.

      Anyway, if we assume that the data survives and
      the server is using SAME Endian Byte Order as you client.
      https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/

      Then you can just open a file on the server and write back the blocks and get the file.

      C 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi and welcome to the forums
        Are you sure QString will handle the binary data well ?
        Normally i use QByteArray as not to get into issues with NULL values.

        Anyway, if we assume that the data survives and
        the server is using SAME Endian Byte Order as you client.
        https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/

        Then you can just open a file on the server and write back the blocks and get the file.

        C Offline
        C Offline
        cerbica
        wrote on last edited by
        #3

        @mrjj said in Read binary file by block:

        ByteArray

        Hi,
        really thanks for your reply.
        Where do you meant to use QByteArray? inside QVector instead of char*?

        And then, how I may append a QByteArray in a QStringList?

        Thanks
        Sarah

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

          Well it would be instead of QStringList.
          Anyway, docs says
          http://doc.qt.io/qt-5/qstring.html#details
          That it can handle zeros might just work.

          I would for test just store the QStringList to binary file (on the client) and see if it matches the
          binary file, you use for input.

          Is this a real application or more like a school project ?
          Normally one would use
          http://doc.qt.io/qt-5/qdatastream.html
          as it handle byte order and is meant for binary data.
          https://stackoverflow.com/questions/26146445/how-to-send-data-from-server-to-client-as-qbytearray-qdatastream/46162082#46162082

          Also why do need to manually split the input ?

          C 1 Reply Last reply
          2
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @cerbica said in Read binary file by block:

            And then, how I may append a QByteArray in a QStringList?

            You can use the base64 representation of the array to make sure it's all ascii data.
            stringList.append(QString::fromLatin1(byteArray.toBase64()));

            "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

            C 1 Reply Last reply
            2
            • VRoninV VRonin

              @cerbica said in Read binary file by block:

              And then, how I may append a QByteArray in a QStringList?

              You can use the base64 representation of the array to make sure it's all ascii data.
              stringList.append(QString::fromLatin1(byteArray.toBase64()));

              C Offline
              C Offline
              cerbica
              wrote on last edited by
              #6

              @VRonin said in Read binary file by block:

              stringList.append(QString::fromLatin1(byteArray.toBase64()));

              Hi,
              are you able to suggest me how to turn back to the binary string on the C server side?

              Thanks
              Sarah

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                To decode the base64 you can use https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c

                No idea how you can reliably deserialise a QString object on the C side though as I think that's an implementation detail (i.e. Qt can change it without warning from one version to the next)

                "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
                0
                • mrjjM mrjj

                  Well it would be instead of QStringList.
                  Anyway, docs says
                  http://doc.qt.io/qt-5/qstring.html#details
                  That it can handle zeros might just work.

                  I would for test just store the QStringList to binary file (on the client) and see if it matches the
                  binary file, you use for input.

                  Is this a real application or more like a school project ?
                  Normally one would use
                  http://doc.qt.io/qt-5/qdatastream.html
                  as it handle byte order and is meant for binary data.
                  https://stackoverflow.com/questions/26146445/how-to-send-data-from-server-to-client-as-qbytearray-qdatastream/46162082#46162082

                  Also why do need to manually split the input ?

                  C Offline
                  C Offline
                  cerbica
                  wrote on last edited by
                  #8

                  Hi @mrjj,
                  It' a real application :)
                  I need to split because the server can handle only 64 bytes at a time.

                  I'm trying to use QByteArray instead of char* and to append the QByteArray as @VRonin suggests.
                  Now I have to turn back to the original binary string on C server side. Any idea?

                  Thanks
                  Sarah

                  VRoninV 1 Reply Last reply
                  0
                  • C cerbica

                    Hi @mrjj,
                    It' a real application :)
                    I need to split because the server can handle only 64 bytes at a time.

                    I'm trying to use QByteArray instead of char* and to append the QByteArray as @VRonin suggests.
                    Now I have to turn back to the original binary string on C server side. Any idea?

                    Thanks
                    Sarah

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @cerbica said in Read binary file by block:

                    Now I have to turn back to the original binary string on C server side

                    Did you already receive it? in what format are you storing it C side?

                    "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
                    0

                    • Login

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