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. Problem with gbinary files, and QVector
QtWS25 Last Chance

Problem with gbinary files, and QVector

Scheduled Pinned Locked Moved General and Desktop
35 Posts 7 Posters 5.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.
  • L Offline
    L Offline
    Loc888
    wrote on last edited by Loc888
    #1

    Hi, i tried to save QVector data, and read it from binary file, but it crashes every time, when i try to access the file.

    If i remember good, it will work, but only if the QVector size is constant, then i can load it back, if i add some elements, save, and load the data, it will crash.

    I think the problem is, i can't save the arrays correctly, because i get the whole one big object, cast it to char, and load it back. I tried to find some good tutorials, but i wasn't unable to save the all the data.

    I use standard STD method, no qfile or something. If someone could give me an example or something, how to do it, i will appreciate it. If you want actual code, i will paste it here, but i don't think it will help, because most likely, it should be something completely different.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It would surely help that you show the code you are using, otherwise it's just going to be shots in the dark as you don't explain what type of data you have in the QVector nor how you manipulate it.

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

      L 2 Replies Last reply
      1
      • SGaistS SGaist

        Hi,

        It would surely help that you show the code you are using, otherwise it's just going to be shots in the dark as you don't explain what type of data you have in the QVector nor how you manipulate it.

        L Offline
        L Offline
        Loc888
        wrote on last edited by VRonin
        #3

        @SGaist
        This is version one.

        #include "Windows/Main_Window/Main_Window.hpp"
        #include "ui_Main_Window.h"
        #include <QDebug>
        
        //***************************************************************************
        //***************************************************************************
        Main_Window::Main_Window(QWidget *parent) : QMainWindow(parent), ui(new Ui::Main_Window)
        {
            ui->setupUi(this);
            this->file_I = new ifstream("Data.bin", ios_base::binary | ios_base::in | ios_base::trunc);
            this->file_O = new ofstream;
        }
        //***************************************************************************
        //***************************************************************************
        Main_Window::~Main_Window()
        {
            this->file_I->close();
            this->file_O->close();
            delete this->file_I;
            delete this->file_O;
            delete ui;
        }
        //***************************************************************************
        //***************************************************************************
        void Main_Window::on_save_button_clicked()
        {
            this->Get_Data();
            this->Write(this->File, this->data);
            this->Show_Data();
        }
        //***************************************************************************
        //***************************************************************************
        void Main_Window::on_load_button_clicked()
        {
            this->Load(this->File, this->data);
            this->Show_Data();
        }
        //***************************************************************************
        //***************************************************************************
        void Main_Window::Write(FILE* file, Data& data)
        {
            file = fopen("Data.bin", "wb");
            fwrite(&data, sizeof(Data), 1, file);
            fclose(file);
        }
        //***************************************************************************
        //***************************************************************************
        void Main_Window::Load(FILE* file, Data& data)
        {
            fopen("Data.bin", "wb");
            fread(&data, sizeof(Data), 1, file);
            fclose(file);
        }
        //***************************************************************************
        //***************************************************************************
        void Main_Window::Show_Data()
        {
            this->ui->table_widget->item(0, 0)->setText(QString::number(this->data.scatter));
            this->ui->table_widget->item(1, 0)->setText(QString::number(this->data.speed));
            this->ui->table_widget->item(2, 0)->setText(QString::fromStdString(this->data.name));
        }
        //***************************************************************************
        //***************************************************************************
        void Main_Window::Get_Data()
        {
            this->data.scatter = this->ui->table_widget->item(0, 0)->text().toInt();
            this->data.speed = this->ui->table_widget->item(1, 0)->text().toInt();
            this->data.name = this->ui->table_widget->item(2, 0)->text().toStdString();
        }
        //***************************************************************************
        //***************************************************************************
        
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          It would surely help that you show the code you are using, otherwise it's just going to be shots in the dark as you don't explain what type of data you have in the QVector nor how you manipulate it.

          L Offline
          L Offline
          Loc888
          wrote on last edited by Loc888
          #4

          @SGaist
          I tried many times, and this crap still don't work.
          ``

          #include "Windows/Main_Window/Main_Window.hpp"
          #include "ui_Main_Window.h"
          #include <QDebug>
          
          //***************************************************************************
          //***************************************************************************
          Main_Window::Main_Window(QWidget *parent) : QMainWindow(parent), ui(new Ui::Main_Window)
          {
              ui->setupUi(this);
              this->file_I = new ifstream("Data.bin", ios_base::binary | ios_base::in | ios_base::trunc);
              this->file_O = new ofstream;
          }
          //***************************************************************************
          //***************************************************************************
          Main_Window::~Main_Window()
          {
              this->file_I->close();
              this->file_O->close();
              delete this->file_I;
              delete this->file_O;
              delete ui;
          }
          //***************************************************************************
          //***************************************************************************
          void Main_Window::on_save_button_clicked()
          {
              this->Get_Data();
              this->Write((*this->file_O), this->data);
              this->Show_Data();
          }
          //***************************************************************************
          //***************************************************************************
          void Main_Window::on_load_button_clicked()
          {
              this->Load((*this->file_I), this->data);
              this->Show_Data();
          }
          //***************************************************************************
          //***************************************************************************
          void Main_Window::Write(ofstream &file, Data& data)
          {
              file.open("Data.bin", ios_base::binary | ios_base::out | ios_base::trunc);
              if(file.is_open() == true)
              {
                  file.write((char*)&data, sizeof(Data));
              }
              file.close();
          }
          //***************************************************************************
          //***************************************************************************
          void Main_Window::Load(ifstream &file, Data& data)
          {
              file.open("Data.bin", ios_base::binary | ios_base::in | ios_base::trunc);
              if(file.is_open() == true)
              {
                  while(!file.eof())
                  {file.read((char*)&data, sizeof(Data));}
              }
              file.close();
          }
          //***************************************************************************
          //***************************************************************************
          void Main_Window::Show_Data()
          {
              this->ui->table_widget->item(0, 0)->setText(QString::number(this->data.scatter));
              this->ui->table_widget->item(1, 0)->setText(QString::number(this->data.speed));
              this->ui->table_widget->item(2, 0)->setText(QString::fromStdString(this->data.name));
          }
          //***************************************************************************
          //***************************************************************************
          void Main_Window::Get_Data()
          {
              this->data.scatter = this->ui->table_widget->item(0, 0)->text().toInt();
              this->data.speed = this->ui->table_widget->item(1, 0)->text().toInt();
              this->data.name = this->ui->table_widget->item(2, 0)->text().toStdString();
          }
          //***************************************************************************
          //***************************************************************************
          
          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            By looking at the code are you writing the Data(which is structure/class) to file ? While writing the user defined objects you need to serialise & write it. Have you seen the [example] ?(http://blog.qt.io/blog/2018/05/31/serialization-in-and-with-qt/). Also I did not see Vector in the code. Did I miss something ?

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            L 1 Reply Last reply
            4
            • dheerendraD dheerendra

              By looking at the code are you writing the Data(which is structure/class) to file ? While writing the user defined objects you need to serialise & write it. Have you seen the [example] ?(http://blog.qt.io/blog/2018/05/31/serialization-in-and-with-qt/). Also I did not see Vector in the code. Did I miss something ?

              L Offline
              L Offline
              Loc888
              wrote on last edited by
              #6

              @dheerendra I notice the problem with saving any data. I can save something, but if i close the program and run it again, then try to read it, the program will always crash.
              I saw many of tutorials, but they are a little bit stupid, what they do, they save the file, and read it immediately, they don't close the program.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                When it's crashing you should use a debugger to see where exactly it is crashing. And since we're a Qt forum I would not use std::io but QFile.

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

                L 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  When it's crashing you should use a debugger to see where exactly it is crashing. And since we're a Qt forum I would not use std::io but QFile.

                  L Offline
                  L Offline
                  Loc888
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher I tried that too, but it won't help. It will be good, is someone could take a look at the load function, because i feel like it's not loading the data correctly.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    First you open the file twice. Second I don't understand why you pass all ifstream and ofstream around around for no good reason. And Third I don't think you want to read a file which gets truncated when it's opened for reading.
                    You should really use a debugger to see where it crashes as I already said in my last post - I don't see what your problem has to do why Qt in any way.

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

                    L 1 Reply Last reply
                    2
                    • Christian EhrlicherC Christian Ehrlicher

                      First you open the file twice. Second I don't understand why you pass all ifstream and ofstream around around for no good reason. And Third I don't think you want to read a file which gets truncated when it's opened for reading.
                      You should really use a debugger to see where it crashes as I already said in my last post - I don't see what your problem has to do why Qt in any way.

                      L Offline
                      L Offline
                      Loc888
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher Listen, if i was good with files, i would not ask for help most likely, this is just a test program and it has nothing to do with real data, and real functionality.

                      I tried it with the second version of the code, that one with ofstream and ifstream, and now it's not crashing but it loads the default values... I have no clue why, it's working like there is no load function, and the data is not loading.

                      So i am running the program, i change the values and save them, exit, then i run it again, click the load button, and i see the default values.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher said in Problem with gbinary files, and QVector:

                        I don't think you want to read a file which gets truncated when it's opened for reading.

                        Did you actually read what ios_base::trunc does?

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

                        L 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @Christian-Ehrlicher said in Problem with gbinary files, and QVector:

                          I don't think you want to read a file which gets truncated when it's opened for reading.

                          Did you actually read what ios_base::trunc does?

                          L Offline
                          L Offline
                          Loc888
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher I remember i tried not using it, and it dosn't help.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by SGaist
                            #13

                            You're truncating your file both when reading and writing. Then you have that data variable but it's unknown whether it's properly initialised.

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

                            L 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              You're truncating your file both when reading and writing. Then you have that data variable but it's unknown whether it's properly initialised.

                              L Offline
                              L Offline
                              Loc888
                              wrote on last edited by Loc888
                              #14

                              @SGaist Ok, i try again, but take in consideration in those tutorials i saw, they use that trunc.
                              I removed that trunc everywhere, now something is working, but the next problem i get, is it's crashing, because of the string "name" variable.
                              What happened, is i get the error, i run the debugger, and it points on the name variable, in the show_data function, if i just comment it, then it's working.

                              How to fix this? I guess it's the stream size fault, because last day i saw a weird error, and saw some weird symbols when i was trying to read the data, and the program crash few seconds later.

                              How to save all those QVectors, stings etc, when the size variable change?

                              mrjjM 1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                I do take that into consideration. You should also take into consideration that if there's something you are not sure about, you should look up the documentation regarding that element rather than blindingly adding things here and there.

                                As suggested before, since you are using Qt, use its facilities like QFile and QDataStream and be done with it. Streaming of QString, QVector etc, is already supported.

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

                                L 1 Reply Last reply
                                3
                                • SGaistS SGaist

                                  I do take that into consideration. You should also take into consideration that if there's something you are not sure about, you should look up the documentation regarding that element rather than blindingly adding things here and there.

                                  As suggested before, since you are using Qt, use its facilities like QFile and QDataStream and be done with it. Streaming of QString, QVector etc, is already supported.

                                  L Offline
                                  L Offline
                                  Loc888
                                  wrote on last edited by
                                  #16

                                  @SGaist I don't know any documentation about files and arrays, if i was able to do it with that, i would not post anything, because there is no point.

                                  aha_1980A 1 Reply Last reply
                                  0
                                  • L Loc888

                                    @SGaist I don't know any documentation about files and arrays, if i was able to do it with that, i would not post anything, because there is no point.

                                    aha_1980A Offline
                                    aha_1980A Offline
                                    aha_1980
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @Loc888 The link to QDataStream that @SGaist gave you contains an almost reusable, yet small example for loading and saving. Have you looked at it?

                                    Regards

                                    Qt has to stay free or it will die.

                                    1 Reply Last reply
                                    1
                                    • L Loc888

                                      @SGaist Ok, i try again, but take in consideration in those tutorials i saw, they use that trunc.
                                      I removed that trunc everywhere, now something is working, but the next problem i get, is it's crashing, because of the string "name" variable.
                                      What happened, is i get the error, i run the debugger, and it points on the name variable, in the show_data function, if i just comment it, then it's working.

                                      How to fix this? I guess it's the stream size fault, because last day i saw a weird error, and saw some weird symbols when i was trying to read the data, and the program crash few seconds later.

                                      How to save all those QVectors, stings etc, when the size variable change?

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @Loc888
                                      Hi
                                      The real benefit of using QFile and QDataStream is that it already can handle many Qt types.
                                      (including QString and QVector)

                                      http://doc.qt.io/qt-5/datastreamformat.html

                                      So you need not to do anything special besides how mini sample shows it

                                      QFile file("file.dat");
                                      file.open(QIODevice::WriteOnly);
                                      QDataStream out(&file); // we will serialize the data into the file
                                      QVector<double> list;
                                      out << list; // serialize a list of doubles. handle size by it self.

                                      However, if you QVector is a list of custom object ( your own class) you need to define
                                      << and >> for it so it know how to stream your types too.
                                      Often that is just to stream the member variables if they in turn are plain Qt types.

                                      L 1 Reply Last reply
                                      2
                                      • mrjjM mrjj

                                        @Loc888
                                        Hi
                                        The real benefit of using QFile and QDataStream is that it already can handle many Qt types.
                                        (including QString and QVector)

                                        http://doc.qt.io/qt-5/datastreamformat.html

                                        So you need not to do anything special besides how mini sample shows it

                                        QFile file("file.dat");
                                        file.open(QIODevice::WriteOnly);
                                        QDataStream out(&file); // we will serialize the data into the file
                                        QVector<double> list;
                                        out << list; // serialize a list of doubles. handle size by it self.

                                        However, if you QVector is a list of custom object ( your own class) you need to define
                                        << and >> for it so it know how to stream your types too.
                                        Often that is just to stream the member variables if they in turn are plain Qt types.

                                        L Offline
                                        L Offline
                                        Loc888
                                        wrote on last edited by
                                        #19

                                        @mrjj Ok, i tried it with simple data, and it's working very good, it's even too easy to use, with comparision to that std stuff. So, about that standard method, if someone could help me learn how to do that, i mean how to read an array i will appreciate it, even if i am gonna use QFile because it's much more cleaner and faster too use.

                                        mrjjM 1 Reply Last reply
                                        0
                                        • L Loc888

                                          @mrjj Ok, i tried it with simple data, and it's working very good, it's even too easy to use, with comparision to that std stuff. So, about that standard method, if someone could help me learn how to do that, i mean how to read an array i will appreciate it, even if i am gonna use QFile because it's much more cleaner and faster too use.

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @Loc888
                                          Hi
                                          yes, its actually nicer than the std stuff.

                                          Well, it can directly do it with QVector.
                                          It actually write the size first then each item but its handled automatically.

                                          When you say array, you mean QVector or what type ?

                                          for any supported Qtype, a list or vector is the same as basic variables.
                                          simply
                                          outstream << thelist;
                                          and
                                          instream >> somelistvar;

                                          Do you have a special case in in mind with the "array" ?
                                          Can you show the declaration of your array ?
                                          makes it easier to talk about.

                                          L 1 Reply Last reply
                                          0

                                          • Login

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