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.8k 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.
  • Z Offline
    Z Offline
    Zgembo
    wrote on 7 Dec 2018, 10:13 last edited by
    #1

    Hi all,

    I am trying to figure out how to write simple struct to binary file.
    Structure looks like this:

    typedef struct
    {
    int PatientID;
    QString Name;
    QString LastName;
    } PatientItem_t;

    What I need it to write multiple patient structures to binary file, and read those structures fro the file.

    Can anyone give any suggestions?

    K 1 Reply Last reply 7 Dec 2018, 10:27
    0
    • Z Zgembo
      7 Dec 2018, 10:13

      Hi all,

      I am trying to figure out how to write simple struct to binary file.
      Structure looks like this:

      typedef struct
      {
      int PatientID;
      QString Name;
      QString LastName;
      } PatientItem_t;

      What I need it to write multiple patient structures to binary file, and read those structures fro the file.

      Can anyone give any suggestions?

      K Offline
      K Offline
      koahnig
      wrote on 7 Dec 2018, 10:27 last edited by
      #2

      @Zgembo

      Best is defining some operator<< and operator>> for teh structure like done in here for instance

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

      1 Reply Last reply
      9
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 7 Dec 2018, 10:48 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
        • Z Offline
          Z Offline
          Zgembo
          wrote on 7 Dec 2018, 13:39 last edited by koahnig 12 Jul 2018, 14:03
          #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 7 Dec 2018, 14:09
          0
          • Z Zgembo
            7 Dec 2018, 13:39

            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 7 Dec 2018, 14:09 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)

            Z 1 Reply Last reply 7 Dec 2018, 14:22
            0
            • K koahnig
              7 Dec 2018, 14:09

              @Zgembo

              Did you use flush or close the file?

              Z Offline
              Z Offline
              Zgembo
              wrote on 7 Dec 2018, 14:22 last edited by
              #6

              @koahnig Yes, I have used flush and close.

              J 1 Reply Last reply 7 Dec 2018, 21:50
              0
              • Z Zgembo
                7 Dec 2018, 14:22

                @koahnig Yes, I have used flush and close.

                J Offline
                J Offline
                JonB
                wrote on 7 Dec 2018, 21:50 last edited by JonB 12 Jul 2018, 21:59
                #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?

                Z 1 Reply Last reply 8 Dec 2018, 15:39
                2
                • J JonB
                  7 Dec 2018, 21:50

                  @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?

                  Z Offline
                  Z Offline
                  Zgembo
                  wrote on 8 Dec 2018, 15:39 last edited by VRonin 12 Oct 2018, 08:34
                  #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 8 Dec 2018, 20:44 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

                    Z 1 Reply Last reply 10 Dec 2018, 07:46
                    0
                    • SGaistS SGaist
                      8 Dec 2018, 20:44

                      Hi,

                      Can you show your header file content ?

                      Z Offline
                      Z Offline
                      Zgembo
                      wrote on 10 Dec 2018, 07:46 last edited by VRonin 12 Oct 2018, 08:35
                      #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 10 Dec 2018, 08:01
                      0
                      • Z Zgembo
                        10 Dec 2018, 07:46

                        @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 10 Dec 2018, 08:01 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 10 Dec 2018, 08:36 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
                          • Z Offline
                            Z Offline
                            Zgembo
                            wrote on 10 Dec 2018, 09:23 last edited by
                            #13

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

                            1 Reply Last reply
                            0

                            1/13

                            7 Dec 2018, 10:13

                            • Login

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