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. Save struct like binary file
Forum Update on Monday, May 27th 2025

Save struct like binary file

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 993 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on 19 May 2020, 16:51 last edited by
    #1

    Hi,

    I have a some problem with crash the application while I try loading the structure from binary file.
    My structure deninition:

    struct Parameters{
    int ID = 0;
    QString name = "Section";
    int temperature_expected = 0;
    };

    I create binary file :

    Parameters parameters;
    
    QFile file(filename);
    if(!file.open(QFile::WriteOnly)){
        qDebug()<<"Cant open file";
        return;
    }
    file.write((char*)&parameters, sizeof(parameters));
    file.flush();
    file.close();
    

    But when i try load file, the application is crashed

    QFile file(path);
    if (!file.open(QIODevice::ReadOnly)){
        qDebug()<<"Cant open load file";
        return false;
    }
    
    QByteArray bytes = file.readAll();
    
    if(bytes.size() != sizeof(Parameters)){
        return false;
    }
    Parameters* parameters = reinterpret_cast<Parameters*>(bytes.data());
    
    return true;
    

    anyone know what could be wrong?

    J W 2 Replies Last reply 19 May 2020, 17:05
    0
    • C Creatorczyk
      19 May 2020, 16:51

      Hi,

      I have a some problem with crash the application while I try loading the structure from binary file.
      My structure deninition:

      struct Parameters{
      int ID = 0;
      QString name = "Section";
      int temperature_expected = 0;
      };

      I create binary file :

      Parameters parameters;
      
      QFile file(filename);
      if(!file.open(QFile::WriteOnly)){
          qDebug()<<"Cant open file";
          return;
      }
      file.write((char*)&parameters, sizeof(parameters));
      file.flush();
      file.close();
      

      But when i try load file, the application is crashed

      QFile file(path);
      if (!file.open(QIODevice::ReadOnly)){
          qDebug()<<"Cant open load file";
          return false;
      }
      
      QByteArray bytes = file.readAll();
      
      if(bytes.size() != sizeof(Parameters)){
          return false;
      }
      Parameters* parameters = reinterpret_cast<Parameters*>(bytes.data());
      
      return true;
      

      anyone know what could be wrong?

      J Offline
      J Offline
      JonB
      wrote on 19 May 2020, 17:05 last edited by JonB
      #2

      @Creatorczyk

      Parameters* parameters = reinterpret_cast<Parameters*>(bytes.data());
      

      You have left parameters pointing into the data() of a QByteArray which has gone out of scope. When you later (presumably) try to read from the content you get rubbish back, including the QString name. If you try to write into *parameters havoc ensues. And you can's store QStrings to file like this anyway.

      Do you still need to do this file stuff in this old way? Serializing (QDataStream) might at least be preferable?

      1 Reply Last reply
      0
      • C Creatorczyk
        19 May 2020, 16:51

        Hi,

        I have a some problem with crash the application while I try loading the structure from binary file.
        My structure deninition:

        struct Parameters{
        int ID = 0;
        QString name = "Section";
        int temperature_expected = 0;
        };

        I create binary file :

        Parameters parameters;
        
        QFile file(filename);
        if(!file.open(QFile::WriteOnly)){
            qDebug()<<"Cant open file";
            return;
        }
        file.write((char*)&parameters, sizeof(parameters));
        file.flush();
        file.close();
        

        But when i try load file, the application is crashed

        QFile file(path);
        if (!file.open(QIODevice::ReadOnly)){
            qDebug()<<"Cant open load file";
            return false;
        }
        
        QByteArray bytes = file.readAll();
        
        if(bytes.size() != sizeof(Parameters)){
            return false;
        }
        Parameters* parameters = reinterpret_cast<Parameters*>(bytes.data());
        
        return true;
        

        anyone know what could be wrong?

        W Offline
        W Offline
        wrosecrans
        wrote on 19 May 2020, 17:09 last edited by
        #3

        Dumping a QString to a file like that won't be useful. It doesn't have the actual "Section" text within the struct, just a pointer. You need to use structs with "plain old data" types and no complicated classes or pointers if you want to be able to usefully persist them like that. If you want text in such a struct, you have to use a simple fixed length character array. If you want variable length text in a binary file, you need a more complicated format where you store a length for the text data used read values one at a time to figure out how long the reads should be..

        1 Reply Last reply
        2
        • V Offline
          V Offline
          VRonin
          wrote on 19 May 2020, 17:13 last edited by VRonin
          #4

          QString stores all its data on the heap so your solution can't work.
          The correct way:

          QDataStream &operator<<(QDataStream& out, const Parameters& par){
          return out << par.id << par.name << par.temperature_expected;
          }
          QDataStream &operator>>(QDataStream& in, Parameters& par){
          return out >> par.id >> par.name >> par.temperature_expected;
          }
          

          QFile file(filename);
          if(!file.open(QFile::WriteOnly)){
              qDebug()<<"Cant open file";
              return;
          }
          QDatastream writeStream(&file);
          writeStream << parameters;
          

          QFile file(path);
          if (!file.open(QIODevice::ReadOnly)){
              qDebug()<<"Cant open load file";
              return false;
          }
          Parameters parameters;
          QDatastream readStream(&file);
          readStream >> parameters;
          

          See https://doc.qt.io/qt-5/qdatastream.html

          "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

          1/4

          19 May 2020, 16:51

          • Login

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