Qt中自定义文件格式,读写二进制文件的串行化类的使用
2018-04-01 10:43
495 查看
http://peipengshuai.blog.163.com/blog/static/19012266201071653752316/
Qt中自定义文件格式,读写二进制文件的串行化类的使用
读写二进制文件的串行化类的使用,具体代码如下:
定义一个结构体:
struct PatientInfor
{
QString m_strID;//病人唯一ID号
QString m_strHospitalID;//病例号(医院编号)
QString m_strName;//病人姓名
QString m_strSurName;//病人姓
QString m_strFirstName;//病人名
QString m_strPY;//病人姓名全拼
int m_Sex;
int m_intAge;//病人年龄
QDate m_DateCreateDate;//病人档案建立日期
QDate m_DatePatientHospitalDate;//住院日期
QString m_strAddress;//住址
QString m_strZip;//邮编
QString m_strTele;//电话
QString m_strCreatorID;//创建用户编号
int m_DeleteStateFlage;
QString m_strRemark;//备注
};
写二进制文件:
void wrietFile(QString strTemp)//strTemp写入文件名
{
QFile fileWrite(strTemp);
fileWrite.open(QIODevice::WriteOnly);
QDataStream writeDataStream(&fileWrite);
PatientInfor *patientInfor = new PatientInfor ();
writeDataStream<<patientInfor->m_strID<<patientInfor->m_strHospitalID<<patientInfor->m_strName<<patientInfor->m_strSurName<<patientInfor->m_strFirstName<<patientInfor->m_strPY<<patientInfor->m_Sex<<patientInfor->m_intAge<<patientInfor->m_DateCreateDate<<patientInfor->m_DatePatientHospitalDate<<patientInfor->m_strAddress<<patientInfor->m_strZip<<patientInfor->m_strTele<<patientInfor->m_strCreatorID<<patientInfor->m_DeleteStateFlage<<patientInfor->m_strRemark;
fileWrite.close();
delete patientInfor;
patientInfor = NULL;
}
读二进制文件:
void readFile(QString strTemp) //strTemp 读文件名
{
QFile fileRead(strTemp);
fileRead.open(QIODevice::ReadOnly);
QDataStream readDataStream(&fileRead);
PatientInfor *readPatientInfor = new PatientInfor();
readDataStream>>readPatientInfor->m_strID>>readPatientInfor->m_strHospitalID>>readPatientInfor->m_strName>>readPatientInfor->m_strSurName>>readPatientInfor->m_strFirstName>>readPatientInfor->m_strPY>>readPatientInfor->m_Sex>>readPatientInfor->m_intAge>>readPatientInfor->m_DateCreateDate>>readPatientInfor->m_DatePatientHopitalDate>>readPatientInfor->m_strAddress>>readPatientInfor->m_strZip>>patientInfor->m_strTele>>readPatientInfor->m_strCreatorID>>readPatientInfor->m_DeleteStateFlage>>readPatientInfor->m_strRemark;
fileRead.close();
delete readPatientInfor;
}
注意:QFile 和QDataStream 类联合使用实现了二进制文件读写的串行化,对读写类型的支持也比较多,对自定义文件的读写提供了极大的方便,但是在读写时要严格保持读写的一致性。
转载自:https://blog.csdn.net/seicany/article/details/22058287
Qt中自定义文件格式,读写二进制文件的串行化类的使用
读写二进制文件的串行化类的使用,具体代码如下:
定义一个结构体:
struct PatientInfor
{
QString m_strID;//病人唯一ID号
QString m_strHospitalID;//病例号(医院编号)
QString m_strName;//病人姓名
QString m_strSurName;//病人姓
QString m_strFirstName;//病人名
QString m_strPY;//病人姓名全拼
int m_Sex;
int m_intAge;//病人年龄
QDate m_DateCreateDate;//病人档案建立日期
QDate m_DatePatientHospitalDate;//住院日期
QString m_strAddress;//住址
QString m_strZip;//邮编
QString m_strTele;//电话
QString m_strCreatorID;//创建用户编号
int m_DeleteStateFlage;
QString m_strRemark;//备注
};
写二进制文件:
void wrietFile(QString strTemp)//strTemp写入文件名
{
QFile fileWrite(strTemp);
fileWrite.open(QIODevice::WriteOnly);
QDataStream writeDataStream(&fileWrite);
PatientInfor *patientInfor = new PatientInfor ();
writeDataStream<<patientInfor->m_strID<<patientInfor->m_strHospitalID<<patientInfor->m_strName<<patientInfor->m_strSurName<<patientInfor->m_strFirstName<<patientInfor->m_strPY<<patientInfor->m_Sex<<patientInfor->m_intAge<<patientInfor->m_DateCreateDate<<patientInfor->m_DatePatientHospitalDate<<patientInfor->m_strAddress<<patientInfor->m_strZip<<patientInfor->m_strTele<<patientInfor->m_strCreatorID<<patientInfor->m_DeleteStateFlage<<patientInfor->m_strRemark;
fileWrite.close();
delete patientInfor;
patientInfor = NULL;
}
读二进制文件:
void readFile(QString strTemp) //strTemp 读文件名
{
QFile fileRead(strTemp);
fileRead.open(QIODevice::ReadOnly);
QDataStream readDataStream(&fileRead);
PatientInfor *readPatientInfor = new PatientInfor();
readDataStream>>readPatientInfor->m_strID>>readPatientInfor->m_strHospitalID>>readPatientInfor->m_strName>>readPatientInfor->m_strSurName>>readPatientInfor->m_strFirstName>>readPatientInfor->m_strPY>>readPatientInfor->m_Sex>>readPatientInfor->m_intAge>>readPatientInfor->m_DateCreateDate>>readPatientInfor->m_DatePatientHopitalDate>>readPatientInfor->m_strAddress>>readPatientInfor->m_strZip>>patientInfor->m_strTele>>readPatientInfor->m_strCreatorID>>readPatientInfor->m_DeleteStateFlage>>readPatientInfor->m_strRemark;
fileRead.close();
delete readPatientInfor;
}
注意:QFile 和QDataStream 类联合使用实现了二进制文件读写的串行化,对读写类型的支持也比较多,对自定义文件的读写提供了极大的方便,但是在读写时要严格保持读写的一致性。
转载自:https://blog.csdn.net/seicany/article/details/22058287
相关文章推荐
- Qt中自定义文件格式,读写二进制文件的串行化类的使用
- Qt中自定义文件格式,读写二进制文件的串行化类的使用
- QT 使用QSettings读写ini配置文件
- highcharts自定义导出文件格式(csv) highcharts的一些使用心得
- 使用Spark读写CSV格式文件(转)
- 使用POI读取xlsx文件,包含对excel中自定义时间格式的处理
- Qt 二进制文件读写(使用“魔术数字”)
- 使用WinPcap和libpcap类库读写pcap文件(002)PCAP文件格式
- 使用Spark读写CSV格式文件
- QT 使用QSettings读写ini配置文件
- Android studio使用自定义的格式化文件或者eclipse的格式文件
- delphi 10.2 创建并使用资源文件(一共22种格式,RCDATA是自定义格式)
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- QT读写ini配置文件以及绝对路径的使用
- QT 使用QSettings 读写ini配置文件
- 使用POI读取XLSX文件,包含对EXCEL中自定义时间格式的处理--包含自己解决和修改jeeplus的源码的
- QT 使用QSettings 读写ini配置文件
- Qt编程4:QSettings使用二(普通ini文件的读写:实际操作)
- Qt串行化的输入和输出(使用QDataStream读写QByteArray,对QIODevice直接起作用)
- C++使用json二维数组格式读写二进制文件