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

Read binary data.

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 3.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.
  • A Offline
    A Offline
    AlvaroS
    wrote on 29 Sept 2016, 10:08 last edited by
    #1

    Hello.
    First of all thanks a lot for reading this post and being able to help.

    I write a binary file like this:

            myFile.write(reinterpret_cast<const char *>(&width), sizeof(uint32_t));
            myFile.write(reinterpret_cast<const char *>(&height), sizeof(uint32_t));
            myFile.write(reinterpret_cast<const char *>(&resx), sizeof(float));
            myFile.write(reinterpret_cast<const char *>(&resy), sizeof(float));
            myFile.write(reinterpret_cast<const char *>(&origx), sizeof(float));
            myFile.write(reinterpret_cast<const char *>(&origy), sizeof(float));
    `
    
    Now I would like to read that binary file and get "width and height" in a int.
    
    How can I do that? just read the 2 first bytes?
    
    Thanks a lot!
    R 1 Reply Last reply 29 Sept 2016, 12:55
    1
    • M Offline
      M Offline
      m.sue
      wrote on 29 Sept 2016, 11:53 last edited by
      #2

      Hi,
      You will need to read the first 8 bytes as you save two 4 byte (uint32 = unsigned 32-bit) integers at the head of the file.
      -Michael.

      A 1 Reply Last reply 30 Sept 2016, 09:07
      2
      • A AlvaroS
        29 Sept 2016, 10:08

        Hello.
        First of all thanks a lot for reading this post and being able to help.

        I write a binary file like this:

                myFile.write(reinterpret_cast<const char *>(&width), sizeof(uint32_t));
                myFile.write(reinterpret_cast<const char *>(&height), sizeof(uint32_t));
                myFile.write(reinterpret_cast<const char *>(&resx), sizeof(float));
                myFile.write(reinterpret_cast<const char *>(&resy), sizeof(float));
                myFile.write(reinterpret_cast<const char *>(&origx), sizeof(float));
                myFile.write(reinterpret_cast<const char *>(&origy), sizeof(float));
        `
        
        Now I would like to read that binary file and get "width and height" in a int.
        
        How can I do that? just read the 2 first bytes?
        
        Thanks a lot!
        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 29 Sept 2016, 12:55 last edited by raven-worx
        #3

        @AlvaroS
        whats the purpose of the binary file?
        If you want to read it again using Qt, then simply use QDataStream which handles the correct sizes (reading and writing) for you.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        4
        • V Offline
          V Offline
          VRonin
          wrote on 29 Sept 2016, 14:08 last edited by
          #4

          reinterpret_cast is the WORST way of serialising EVER!

          You can use QDataStream the Qt way or just using standard C++ fstream. There is no justification EVER to use reinterpret_cast. it's just bad programming

          "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

          K 1 Reply Last reply 29 Sept 2016, 17:15
          4
          • V VRonin
            29 Sept 2016, 14:08

            reinterpret_cast is the WORST way of serialising EVER!

            You can use QDataStream the Qt way or just using standard C++ fstream. There is no justification EVER to use reinterpret_cast. it's just bad programming

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 29 Sept 2016, 17:15 last edited by
            #5

            @VRonin said in Read binary data.:

            There is no justification EVER to use reinterpret_cast. it's just bad programming

            There is, in some fringe cases, but I relate to the sentiment. ;)

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1
            • M m.sue
              29 Sept 2016, 11:53

              Hi,
              You will need to read the first 8 bytes as you save two 4 byte (uint32 = unsigned 32-bit) integers at the head of the file.
              -Michael.

              A Offline
              A Offline
              AlvaroS
              wrote on 30 Sept 2016, 09:07 last edited by
              #6

              @m.sue Thanks a lot for answer me!

              I have tried to read as:

              int width;
              int height;

                          infile.read((char*)(width), sizeof(uint32_t)*(2*1));
              

              But it fives me segmetation fault in "infile.read...."

              1 Reply Last reply
              0
              • M Offline
                M Offline
                m.sue
                wrote on 30 Sept 2016, 09:16 last edited by
                #7

                This should work (if it compiles :-):

                infile.read((char*)(&width), sizeof(uint32_t));
                infile.read((char*)(&height), sizeof(uint32_t));
                

                -Michael.

                A 1 Reply Last reply 3 Oct 2016, 09:48
                1
                • M m.sue
                  30 Sept 2016, 09:16

                  This should work (if it compiles :-):

                  infile.read((char*)(&width), sizeof(uint32_t));
                  infile.read((char*)(&height), sizeof(uint32_t));
                  

                  -Michael.

                  A Offline
                  A Offline
                  AlvaroS
                  wrote on 3 Oct 2016, 09:48 last edited by
                  #8

                  @m.sue It works!! Thanks!

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 3 Oct 2016, 10:19 last edited by VRonin 10 Mar 2016, 10:43
                    #9

                    for whoever comes here, the correct way is:

                    Using Qt (myFile is QFile*)

                    // Write
                    QDataStream writeStream(myFile);
                    writeStream << width << height << resx<< resy<< origx<< origy;
                    
                    
                    // Read
                    QDataStream readStream(myFile);
                    readStream>> width >> height >> resx>> resy>> origx>> origy;
                    

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

                    9/9

                    3 Oct 2016, 10:19

                    • Login

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