Write/read struct to binary file using QDataStream



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



  • @Zgembo

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


  • Qt Champions 2017

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



  • 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]



  • @Zgembo

    Did you use flush or close the file?



  • @koahnig Yes, I have used flush and close.



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



  • @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;
    }
    

  • Lifetime Qt Champion

    Hi,

    Can you show your header file content ?



  • @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;
    };
    

  • Moderators

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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.