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 from file

Read binary data from file

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 2.8k 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.
  • H Offline
    H Offline
    HB76
    wrote on 27 Mar 2020, 14:24 last edited by
    #1

    Hi everyone !

    I am trying to read data from a file that can be either in ascii or binary format. For the ascii one, I figured out a code to read it and get the data. But I'm stuck with the binary format.

    My binary file contain a 80 bytes header wich is a string, and then a 4 bytes unigned int that correspond to the data length, followed by the data. I've tried differents approach :

    QFile file(file_name);
    
    if (!file.open(QIODevice::ReadOnly))
            return;
    
    QDataStream s(&file);
    s.setByteOrder(QDataStream::LittleEndian);
    char *buffer = new char[4];
    
    s.skipRawData(80);    // I've already read the header before to determine the file format
    s.readRawData(buffer,4);
    ulong length = ulong(buffer);
    qDebug() << length;
    

    But the value returned is totaly inconsistent and change each time I run my code, wich makes me seriously doubt its validity.
    Other way tested :

    file.seek(80);
    ulong length =  file.read(4).toULong();
    qDebug() << length;
    

    But it return 0 every time.
    And the last one :

    QDataStream s(&file);
    s.setByteOrder(QDataStream::LittleEndian);
    
    ulong length;
    s >> length;
    qDebug() << length;
    

    But it also return 0.

    Do you have any idea of what I am doing wrong ? If differents ways exist, wich one is the fastest ?
    Thank you for your help !

    HB76

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 Mar 2020, 14:54 last edited by
      #2

      Hi,

      @HB76 said in Read binary data from file:

      ulong length = ulong(buffer);

      You are casting a pointer to a char array to ulong not its content. That's why you get a different value every time. You are seeing the address of the first item of that array.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • R Offline
        R Offline
        Rondog
        wrote on 27 Mar 2020, 15:28 last edited by Rondog
        #3

        You can use pointers to do this sort of thing. I have an example that takes some part of a QByteArray and extracts an integer from it:

        int i;
        char *c;
        		
        c = (char*)(&i); // set address of char to point to address of integer.  The two are now tied together
        	
        // indirectly copy bytes to integer.  Var 'index' is first byte of integer value in the byte_array
        for(unsigned int cntr = 0;cntr < sizeof(int);++cntr)
            *(c + cntr) = array[index + cntr];
        	
        // var 'i' now contains integer value
        
        1 Reply Last reply
        0
        • H Offline
          H Offline
          HB76
          wrote on 27 Mar 2020, 15:59 last edited by HB76
          #4

          Thank you for your response,

          indeed I noticed the pointer problem in my code, but even when I use the normal variable, the value returned is not correct :

          QDataStream s(&file);
          s.setByteOrder(QDataStream::LittleEndian);
          char buffer[4];
          
          s.skipRawData(80);
          s.readRawData(buffer,4);
          ulong length = ulong(buffer);
          qDebug() << length;
          

          Should return 70540 in this case but return 2686300.
          Same for :

          QDataStream s(&file);
          s.setByteOrder(QDataStream::LittleEndian);
          char *buffer = new char[4];
          
          s.skipRawData(80);
          s.readRawData(buffer,4);
          ulong length = ulong(*buffer);
          qDebug() << length;
          

          wich return 0...

          I would just like to understand what is incorrect in this code to know what I am doing wrong

          J 1 Reply Last reply 27 Mar 2020, 16:06
          0
          • H HB76
            27 Mar 2020, 15:59

            Thank you for your response,

            indeed I noticed the pointer problem in my code, but even when I use the normal variable, the value returned is not correct :

            QDataStream s(&file);
            s.setByteOrder(QDataStream::LittleEndian);
            char buffer[4];
            
            s.skipRawData(80);
            s.readRawData(buffer,4);
            ulong length = ulong(buffer);
            qDebug() << length;
            

            Should return 70540 in this case but return 2686300.
            Same for :

            QDataStream s(&file);
            s.setByteOrder(QDataStream::LittleEndian);
            char *buffer = new char[4];
            
            s.skipRawData(80);
            s.readRawData(buffer,4);
            ulong length = ulong(*buffer);
            qDebug() << length;
            

            wich return 0...

            I would just like to understand what is incorrect in this code to know what I am doing wrong

            J Offline
            J Offline
            JonB
            wrote on 27 Mar 2020, 16:06 last edited by JonB
            #5

            @HB76 said in Read binary data from file:
            ulong length = ulong(*buffer);

            only looks at the first char/byte in buffer, i.e. buffer[0], and that's probably a 0, so that's not much good!

            As for ulong length = ulong(buffer); not returning what you expect, the first thing I'd do is print it out in hex rather than decimal/look at the 4 bytes and see if they are the wrong way round/not what you expect in the 4 bytes. [I'm tired now, but doesn't ulong(buffer) just do the same thing as (ulong)buffer, and hence all you're printing is the address of the variable and nothing about its content?]

            Apart from that, and I haven't looked up the mechanics, but I don't see how QDataStream::setByteOrder() can cooperate with your choice of using QDataStream::readRawData(). If it reads raw bytes it's not going to be dealing with numbers and observing any byte ordering. Presumably you have to use QDataStream &QDataStream::operator>>(quint32 &i), https://doc.qt.io/qt-5/qdatastream.html#operator-gt-gt-5, if you want to read what you know to be numbers and have the byte order taken into account.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              HB76
              wrote on 27 Mar 2020, 16:27 last edited by
              #6
              QDataStream s(&file);
              s.setByteOrder(QDataStream::LittleEndian);
              
              s.skipRawData(80);
              qint32 value;
              s >> value;
              qDebug() << value;
              

              give me the same result, 0...

              P 1 Reply Last reply 27 Mar 2020, 16:37
              0
              • H HB76
                27 Mar 2020, 16:27
                QDataStream s(&file);
                s.setByteOrder(QDataStream::LittleEndian);
                
                s.skipRawData(80);
                qint32 value;
                s >> value;
                qDebug() << value;
                

                give me the same result, 0...

                P Offline
                P Offline
                Pablo J. Rogina
                wrote on 27 Mar 2020, 16:37 last edited by
                #7

                @HB76 just in case, could you please post:

                1. the values of the bytes at positions 80, 81, 82, 83 for the file you're working with?
                2. the size of the data (or the whole file) for such file?

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  HB76
                  wrote on 27 Mar 2020, 16:39 last edited by
                  #8

                  I've found a way to do it :

                  QByteArray data = file.read(4);
                  qint32 facet_count;
                  memcpy(&facet_count, data.constData(), 4);
                  qDebug() << facet_count;
                  

                  Just for knowledge, why this simple code doesn'y work ?

                  file.seek(80);
                  ulong length =  file.read(4).toULong();
                  qDebug() << length;
                  

                  it would be much more simplier..

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 27 Mar 2020, 16:43 last edited by
                    #9

                    @HB76 said in Read binary data from file:

                    ulong length = file.read(4).toULong();

                    Because you did not read the documentation: https://doc.qt.io/qt-5/qbytearray.html#toLong

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    H 1 Reply Last reply 27 Mar 2020, 16:46
                    2
                    • H Offline
                      H Offline
                      HB76
                      wrote on 27 Mar 2020, 16:43 last edited by
                      #10

                      @Pablo-J-Rogina the values are :

                      80 : "\x8C"
                      81 : "\x13"
                      82 : "\x01"
                      83 : "\x00"

                      the file length is 3527084 using

                      int length = int(file.size());
                      
                      1 Reply Last reply
                      0
                      • C Christian Ehrlicher
                        27 Mar 2020, 16:43

                        @HB76 said in Read binary data from file:

                        ulong length = file.read(4).toULong();

                        Because you did not read the documentation: https://doc.qt.io/qt-5/qbytearray.html#toLong

                        H Offline
                        H Offline
                        HB76
                        wrote on 27 Mar 2020, 16:46 last edited by
                        #11

                        @Christian-Ehrlicher I was just having a look at it but I supposed I didn't understand the whole explaination the first time as I'm not native english ^^'

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 27 Mar 2020, 16:47 last edited by Christian Ehrlicher
                          #12

                          The example there should be obvious:

                          QByteArray str("FF");
                          bool ok;
                          int hex = str.toInt(&ok, 16);     // hex == 255, ok == true
                          int dec = str.toInt(&ok, 10);     // dec == 0, ok == false
                          

                          As you can see it converts a string value into an integer

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            HB76
                            wrote on 27 Mar 2020, 16:51 last edited by
                            #13

                            ok I got it now, but why every time the conversion failed with this method whereas the conversion with memcpy is working ?

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 27 Mar 2020, 16:57 last edited by
                              #14

                              @HB76 said in Read binary data from file:

                              ut why every time the conversion failed with this method whereas the conversion with memcpy is working ?

                              Again: toUint() interprets your string as ascii text and tries to convert it to an integer, whereas memcpy simply copies the plain data - C basics on how a value is interpreted.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              2
                              • H Offline
                                H Offline
                                HB76
                                wrote on 27 Mar 2020, 16:59 last edited by
                                #15

                                Ok thank you very much !
                                I don't know if it is the most efficient way to read data but it is working !

                                1 Reply Last reply
                                0

                                5/15

                                27 Mar 2020, 16:06

                                topic:navigator.unread, 10
                                • Login

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