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
Qt 6.11 is out! See what's new in the release blog

Read Binary Data into a QVector

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 7.1k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on 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 last edited by BadHombre
      #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

      VRoninV 1 Reply Last reply
      0
      • B BadHombre

        @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

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #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 last edited by
          #5

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

          VRoninV 1 Reply Last reply
          0
          • B BadHombre

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

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on 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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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 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.HilkJ 1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on 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

                    @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.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #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 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
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on 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 last edited by BadHombre
                          #13

                          @VRonin I get an error

                          Error using fwrite
                          Invalid precision.

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

                          VRoninV 1 Reply Last reply
                          0
                          • B BadHombre

                            @VRonin I get an error

                            Error using fwrite
                            Invalid precision.

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

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on 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 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.HilkJ VRoninV 2 Replies Last reply
                              0
                              • B BadHombre

                                @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.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on 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

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

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by VRonin
                                  #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

                                  • Login

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