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.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on 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!
    raven-worxR 1 Reply Last reply
    1
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on 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.

      AlvaroSA 1 Reply Last reply
      2
      • AlvaroSA AlvaroS

        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!
        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on 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
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on 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

          kshegunovK 1 Reply Last reply
          4
          • VRoninV VRonin

            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

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on 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.sueM m.sue

              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.

              AlvaroSA Offline
              AlvaroSA Offline
              AlvaroS
              wrote on 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.sueM Offline
                m.sueM Offline
                m.sue
                wrote on 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.

                AlvaroSA 1 Reply Last reply
                1
                • m.sueM m.sue

                  This should work (if it compiles :-):

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

                  -Michael.

                  AlvaroSA Offline
                  AlvaroSA Offline
                  AlvaroS
                  wrote on last edited by
                  #8

                  @m.sue It works!! Thanks!

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #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

                    • Login

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