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

Read Binary Data into a QVector

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 4.4k 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.
  • B Offline
    B Offline
    BadHombre
    wrote on 10 Mar 2017, 08:40 last edited by BadHombre 3 Oct 2017, 08:51
    #1

    Hi Guys,

    I need some help with reading a binary Data into a vector of doubles :/

    This is my Code :

    QString filename = "C:/Qt/Test/test.bin";
     QFile file1(filename);
        if (!file1.open(QIODevice::ReadOnly)) return;
        QByteArray data = file1.readAll();
        qDebug() << data.size();
        QDataStream ds(&data, QIODevice::ReadWrite);
    
           QVector<double> outVect;
           outVect.resize(data.size());
    
    
           ds.readRawData((char*)outVect.data(), data.size());
           foreach (double current, outVect)
               qDebug() << current;
    
    

    My Problem is that i only get 0 :/ and i don't know why --- has sb an idea ?
    Maybe it has sth to do with the line :
    ds.readRawData((char*)outVect.data(), data.size());

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 10 Mar 2017, 08:44 last edited by
      #2

      What is the format of the file? Binary is the most generic description I can think of

      Good place to start http://www.bogotobogo.com/Qt/Qt5_QFile_Serialization.php

      "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
      2
      • B Offline
        B Offline
        BadHombre
        wrote on 10 Mar 2017, 09:03 last edited by BadHombre 3 Oct 2017, 09:04
        #3

        @VRonin thanks for the link :) it's almost the same i have done in my code except the copy intoa vector of double... and thats where i don't know how to do it :/
        the format is a .bin File ... if you open the file with the editor you can see some random ASCII code.. which describes some double values

        V 1 Reply Last reply 10 Mar 2017, 09:13
        0
        • B BadHombre
          10 Mar 2017, 09:03

          @VRonin thanks for the link :) it's almost the same i have done in my code except the copy intoa vector of double... and thats where i don't know how to do it :/
          the format is a .bin File ... if you open the file with the editor you can see some random ASCII code.. which describes some double values

          V Offline
          V Offline
          VRonin
          wrote on 10 Mar 2017, 09:13 last edited by VRonin 3 Oct 2017, 09:25
          #4

          @BadHombre said in Read Binary Data into a QVector:

          thanks for the link :) it's almost the same i have done in my code except the copy intoa vector of double

          No, it's not I'm afraid. It most definitely is not.

          the format is a .bin File

          still meaningless. How is it saved? by who?

          let's try:

          QFile file1("C:/Qt/Test/test.bin");
          QVector<double> outVect;
          if (file1.open(QIODevice::ReadOnly)){
          QDataStream fileStream(&file1);
          for(;;){
          double tempValue;
          fileStream.startTransaction();
          fileStream >> tempValue;
          if(fileStream.commitTransaction())
          outVect << tempValue;
          else
          break;
          }
          }
          

          but it's pure speculation until we know what the file contains.

          If you really have no idea and the file is not too big you can open it in a hex editor and paste the results here so we can try and guess what's in there

          "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
          1
          • B Offline
            B Offline
            BadHombre
            wrote on 10 Mar 2017, 09:17 last edited by
            #5

            the .bin File was created by Matlab and contains only numbers

            V 1 Reply Last reply 10 Mar 2017, 09:25
            0
            • B BadHombre
              10 Mar 2017, 09:17

              the .bin File was created by Matlab and contains only numbers

              V Offline
              V Offline
              VRonin
              wrote on 10 Mar 2017, 09:25 last edited by
              #6

              @BadHombre Getting warmer, can you post the matlab code that created it?

              "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
              • J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 10 Mar 2017, 09:30 last edited by
                #7

                Without seeing more,

                I would suggest union

                union
                {
                    double f;
                    unsigned char chr;
                } u;
                

                this should do the trick as long as you pass the correct byteArrey/length.

                code: untested from my side


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BadHombre
                  wrote on 10 Mar 2017, 09:32 last edited by
                  #8

                  @VRonin so i tested your code but i don ' t get the right values :/

                  matlab code : 
                  matrix_save=[A' B' C D' E' ];
                  fileID = fopen('folder/filename.bin','w');
                  fwrite(fileID,matrix_save','double');
                  fclose(fileID);
                  
                  

                  this is the code i wrote for c++ .. A B C D E are some Matrix..with numbers in it

                    ifstream file("E:\\file.bin");
                  
                          char * buffer;
                          long size;
                          file.seekg(0, std::ios::end);
                          size = file.tellg();
                          file.seekg(0, std::ios::beg);
                          buffer = new char[size];
                          file.read(buffer, size);
                          file.close();
                  
                          double* double_values = (double*)buffer;//reinterpret as doubles
                          vector<double> buffer2(double_values, double_values + (size / sizeof(double)));
                  
                  J 1 Reply Last reply 10 Mar 2017, 09:40
                  0
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 10 Mar 2017, 09:36 last edited by
                    #9

                    the code I posted above should work then

                    "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
                    • B BadHombre
                      10 Mar 2017, 09:32

                      @VRonin so i tested your code but i don ' t get the right values :/

                      matlab code : 
                      matrix_save=[A' B' C D' E' ];
                      fileID = fopen('folder/filename.bin','w');
                      fwrite(fileID,matrix_save','double');
                      fclose(fileID);
                      
                      

                      this is the code i wrote for c++ .. A B C D E are some Matrix..with numbers in it

                        ifstream file("E:\\file.bin");
                      
                              char * buffer;
                              long size;
                              file.seekg(0, std::ios::end);
                              size = file.tellg();
                              file.seekg(0, std::ios::beg);
                              buffer = new char[size];
                              file.read(buffer, size);
                              file.close();
                      
                              double* double_values = (double*)buffer;//reinterpret as doubles
                              vector<double> buffer2(double_values, double_values + (size / sizeof(double)));
                      
                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 10 Mar 2017, 09:40 last edited by J.Hilk 3 Oct 2017, 09:43
                      #10

                      @BadHombre
                      I totally missed, that you're using <fsteam>

                      You could also use this code sample:

                      ifstream file("E:\\file.bin");
                      double read;
                      
                              if ( file) {
                                  file.read( reinterpret_cast<char*>( &read ), sizeof read );
                                  qDebug() << read;
                              }
                      

                      If MatLap has the same byteorder of course :)

                      Edit, I acutally found the Source, from when i was researching that.


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      1
                      • B Offline
                        B Offline
                        BadHombre
                        wrote on 10 Mar 2017, 09:54 last edited by
                        #11

                        @VRonin doesn't work :/

                        this are the values in the Qvector

                        0
                        0
                        3.1568e-58
                        -3.03415e-298
                        2.08159e+74
                        2.92534e+211
                        -2.01411e+84
                        3.21098e+39
                        0
                        0
                        

                        and this are the numbers from matlab which it should have :/

                        0	0	-5,99983766238420	-5,99651426671551	-5,22786749086602	5,99821019538431	5,99832392614572	5,22649512993718	0	0
                        
                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          VRonin
                          wrote on 10 Mar 2017, 10:00 last edited by
                          #12

                          If you can temporarily change the matlab file with from fwrite(fileID,matrix_save','double'); to fwrite(fileID,matrix_save','b'); and see if it works in that case?

                          "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
                          • B Offline
                            B Offline
                            BadHombre
                            wrote on 10 Mar 2017, 10:04 last edited by BadHombre 3 Oct 2017, 10:04
                            #13

                            @VRonin I get an error

                            Error using fwrite
                            Invalid precision.

                            Error in Untitled (line 28)
                            fwrite(fileID,matrix_save','b');

                            V 1 Reply Last reply 10 Mar 2017, 10:06
                            0
                            • B BadHombre
                              10 Mar 2017, 10:04

                              @VRonin I get an error

                              Error using fwrite
                              Invalid precision.

                              Error in Untitled (line 28)
                              fwrite(fileID,matrix_save','b');

                              V Offline
                              V Offline
                              VRonin
                              wrote on 10 Mar 2017, 10:06 last edited by
                              #14

                              @BadHombre my bad, I deleted an argument by mistake, I meant fwrite(fileID,matrix_save','double','b');

                              "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
                              2
                              • B Offline
                                B Offline
                                BadHombre
                                wrote on 10 Mar 2017, 10:09 last edited by
                                #15

                                @VRonin now i get the right number ... what was the Problem ? Is it possible to get the right numbers without changing the matlab code ?

                                J V 2 Replies Last reply 10 Mar 2017, 10:17
                                0
                                • B BadHombre
                                  10 Mar 2017, 10:09

                                  @VRonin now i get the right number ... what was the Problem ? Is it possible to get the right numbers without changing the matlab code ?

                                  J Offline
                                  J Offline
                                  J.Hilk
                                  Moderators
                                  wrote on 10 Mar 2017, 10:17 last edited by
                                  #16

                                  @BadHombre

                                  the b paramter tells matlab to write in Big-endian ordering


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  3
                                  • B BadHombre
                                    10 Mar 2017, 10:09

                                    @VRonin now i get the right number ... what was the Problem ? Is it possible to get the right numbers without changing the matlab code ?

                                    V Offline
                                    V Offline
                                    VRonin
                                    wrote on 10 Mar 2017, 10:30 last edited by VRonin 3 Oct 2017, 10:39
                                    #17

                                    @BadHombre said in Read Binary Data into a QVector:

                                    what was the Problem ?

                                    @J-Hilk is super correct!

                                    Is it possible to get the right numbers without changing the matlab code ?

                                    Yes and no. If your matlab code and your program are assured to run on the same system and you know the default byte order of that system yes.

                                    if you know your system is big-endian then my original code will just work, if you know your system is little endian (like in your case) add fileStream.setByteOrder(QDataStream::LittleEndian); before the for(;;)

                                    There is no way with your current matlab code to make the file readable in another system without a doubt

                                    EDIT

                                    Actually you can hack your way around the "you know the default byte order of that system" but there is no way out of the "your matlab code and your program are assured to run on the same system" part

                                    bool is_big_endian()
                                    {
                                        union {
                                            quint32 i;
                                            char c[4];
                                        } bint = {0x01020304};
                                        return bint.c[0] == 1; 
                                    }
                                    
                                    QFile file1("C:/Qt/Test/test.bin");
                                    QVector<double> outVect;
                                    if (file1.open(QIODevice::ReadOnly)){
                                    QDataStream fileStream(&file1);
                                    if(!is_big_endian())
                                    fileStream.setByteOrder(QDataStream::LittleEndian);
                                    for(;;){
                                    double tempValue;
                                    fileStream.startTransaction();
                                    fileStream >> tempValue;
                                    if(fileStream.commitTransaction())
                                    outVect << tempValue;
                                    else
                                    break;
                                    }
                                    }
                                    

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

                                    1/17

                                    10 Mar 2017, 08:40

                                    • Login

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