Write/read struct to binary file using QDataStream
-
wrote on 7 Dec 2018, 10:13 last edited by
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?
-
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?
wrote on 7 Dec 2018, 10:27 last edited byBest is defining some operator<< and operator>> for teh structure like done in here for instance
-
@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.
-
wrote on 7 Dec 2018, 13:39 last edited by koahnig 12 Jul 2018, 14:03
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]
-
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]
-
wrote on 7 Dec 2018, 21:50 last edited by JonB 12 Jul 2018, 21:59
@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? AndreadPatientExportFile
is deffo opened correctly on it, right? -
@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? AndreadPatientExportFile
is deffo opened correctly on it, right?wrote on 8 Dec 2018, 15:39 last edited by VRonin 12 Oct 2018, 08:34@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; }
-
Hi,
Can you show your header file content ?
-
wrote on 10 Dec 2018, 07:46 last edited by VRonin 12 Oct 2018, 08:35
#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; };
-
#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; };
@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.
-
wrote on 10 Dec 2018, 08:36 last edited by
Why would you need them as
friend
s ofMainWindow
anyway? just declare/define them as global functions
1/13