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. Write/read struct to binary file using QDataStream
Forum Updated to NodeBB v4.3 + New Features

Write/read struct to binary file using QDataStream

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 7 Posters 4.9k Views 2 Watching
  • 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #3

    @koahnig has already given you good link to work on this. In general structure cannot be directly written/read from file. Structure has to be serealized & then written.

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

    1 Reply Last reply
    0
    • ZgemboZ Offline
      ZgemboZ Offline
      Zgembo
      wrote on last edited by koahnig
      #4

      I have managed to overload << operator and to write data to binary file, but when I read data from the file I do not get anything.

      QDataStream & operator<<(QDataStream & str, const PatientItem_t & patient)
      {
      	str << patient.PatientID
      		<< patient.Name
      		<< patient.LastName;
      
      	return str;
      }
      
      QDataStream & operator>>(QDataStream & str, PatientItem_t & patient)
      {
      	str >> patient.PatientID
      		>> patient.Name
      		>> patient.LastName;
      	return str;
      }
      
      PatientItem_t readPatient;
      QDataStream readPatientStream(&readPatientExportFile);
      readPatientStream >> readPatient;
      

      [koahnig: code tags added]

      K 1 Reply Last reply
      0
      • ZgemboZ Zgembo

        I have managed to overload << operator and to write data to binary file, but when I read data from the file I do not get anything.

        QDataStream & operator<<(QDataStream & str, const PatientItem_t & patient)
        {
        	str << patient.PatientID
        		<< patient.Name
        		<< patient.LastName;
        
        	return str;
        }
        
        QDataStream & operator>>(QDataStream & str, PatientItem_t & patient)
        {
        	str >> patient.PatientID
        		>> patient.Name
        		>> patient.LastName;
        	return str;
        }
        
        PatientItem_t readPatient;
        QDataStream readPatientStream(&readPatientExportFile);
        readPatientStream >> readPatient;
        

        [koahnig: code tags added]

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #5

        @Zgembo

        Did you use flush or close the file?

        Vote the answer(s) that helped you to solve your issue(s)

        ZgemboZ 1 Reply Last reply
        0
        • K koahnig

          @Zgembo

          Did you use flush or close the file?

          ZgemboZ Offline
          ZgemboZ Offline
          Zgembo
          wrote on last edited by
          #6

          @koahnig Yes, I have used flush and close.

          JonBJ 1 Reply Last reply
          0
          • ZgemboZ Zgembo

            @koahnig Yes, I have used flush and close.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #7

            @Zgembo
            And you're saying the problem is on input, right? You have verified the output went to the file before you try to read from it, otherwise you would have told us if the file was empty, right? And readPatientExportFile is deffo opened correctly on it, right?

            ZgemboZ 1 Reply Last reply
            2
            • JonBJ JonB

              @Zgembo
              And you're saying the problem is on input, right? You have verified the output went to the file before you try to read from it, otherwise you would have told us if the file was empty, right? And readPatientExportFile is deffo opened correctly on it, right?

              ZgemboZ Offline
              ZgemboZ Offline
              Zgembo
              wrote on last edited by VRonin
              #8

              @JonB Well after further investigation I found out that my overloaded operators << and >> are not called at all. I do not know why.

              In my .h fajl under public members I have put

              friend QDataStream &operator<<(QDataStream& out, const PatientItem_t & patient);
              friend QDataStream &operator>>(QDataStream& in, PatientItem_t & patient);
              

              and at the end of my .cpp fajl I have put:

              QDataStream & operator<<(QDataStream &out, const PatientItem_t &patient)
              {
              	out << patient.PatientID
              		<< patient.Name
              		<< patient.LastName;
              	return out;
              }
              
              QDataStream & operator>>(QDataStream & in, PatientItem_t & patient)
              {
              	in >> patient.PatientID
              		>> patient.Name
              		>> patient.LastName;
              	return in;
              }
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #9

                Hi,

                Can you show your header file content ?

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

                ZgemboZ 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  Can you show your header file content ?

                  ZgemboZ Offline
                  ZgemboZ Offline
                  Zgembo
                  wrote on last edited by VRonin
                  #10

                  @SGaist

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  #include <QDataStream>
                  #include <QFile>
                  #include <QDate>
                  #include <QDebug>
                  
                  namespace Ui {
                  class MainWindow;
                  }
                  
                  typedef struct
                  {
                      int PatientID;
                      QString Name;
                      QString LastName;
                  
                  } PatientItem_t;
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      explicit MainWindow(QWidget *parent = 0);
                      ~MainWindow();
                  
                      friend QDataStream &operator <<(QDataStream &stream,const PatientItem_t &patient);
                      friend QDataStream &operator >>(QDataStream &stream, PatientItem_t &patient);
                  
                  private:
                      Ui::MainWindow *ui;
                  };
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • ZgemboZ Zgembo

                    @SGaist

                    #ifndef MAINWINDOW_H
                    #define MAINWINDOW_H
                    
                    #include <QMainWindow>
                    #include <QDataStream>
                    #include <QFile>
                    #include <QDate>
                    #include <QDebug>
                    
                    namespace Ui {
                    class MainWindow;
                    }
                    
                    typedef struct
                    {
                        int PatientID;
                        QString Name;
                        QString LastName;
                    
                    } PatientItem_t;
                    
                    class MainWindow : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        explicit MainWindow(QWidget *parent = 0);
                        ~MainWindow();
                    
                        friend QDataStream &operator <<(QDataStream &stream,const PatientItem_t &patient);
                        friend QDataStream &operator >>(QDataStream &stream, PatientItem_t &patient);
                    
                    private:
                        Ui::MainWindow *ui;
                    };
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @Zgembo said in Write/read struct to binary file using QDataStream:

                    friend QDataStream &operator <<(QDataStream &stream,const PatientItem_t &patient);
                    friend QDataStream &operator >>(QDataStream &stream, PatientItem_t &patient);

                    You should have those declared outside of the class as well.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #12

                      Why would you need them as friends of MainWindow anyway? just declare/define them as global functions

                      "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
                      3
                      • ZgemboZ Offline
                        ZgemboZ Offline
                        Zgembo
                        wrote on last edited by
                        #13

                        Thank you @jsulm and @VRonin . It works now!

                        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