您的位置:首页 > 编程语言 > Qt开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐