首页 / C++ / c++/qt的数据序列化和反序列化
c++/qt的数据序列化和反序列化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c++/qt的数据序列化和反序列化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1556字,纯文字阅读大概需要3分钟。
内容图文
![c++/qt的数据序列化和反序列化](/upload/InfoBanner/zyjiaocheng/1081/9564bb19950945548a079d8511d67b61.jpg)
序列化以及反序列化的实现
struct Body
{
double weight;
double height;
};
//结构体
struct People
{
int age;
Body dBody;//结构体
vector<QString> vecfamily;//vector
//序列化
friend QDataStream &operator<<(QDataStream& input,const People &iteam)
{
//vector 数据类型需要用vector<People>::fromStdVector 转一下
//如果是QList则不需要直接插入
QVector<QString> strvecfamily = QVector<QString>::fromStdVector(iteam.vecfamily);
input << iteam.age << iteam.dBody.height << iteam.dBody.weight\
<< strvecfamily;
return input;
}
//反序列化
friend QDataStream &operator>>(QDataStream& output,People& iteam)
{
QVector<QString> vecfamily;
output >> iteam.age >>iteam.dBody.height >> iteam.dBody.weight >> vecfamily;
iteam.vecfamily = vecfamily.toStdVector();
return output;
}
};
数据的使用
People p1;
p1.age = 12;
p1.dBody.height = 150.8;
p1.dBody.weight = 50;
p1.vecfamily.push_back("sister");
p1.vecfamily.push_back("mother");
p1.vecfamily.push_back("father");
QFile file("./stream.bat");
if(!file.open(QIODevice::WriteOnly))
{
return ;
}
QDataStream input(&file);
input << p1;
file.close();
People p2;
QFile file1("./stream.bat");//.bat文件为二进制文件
if(!file1.open(QIODevice::ReadOnly))
{
return ;
}
QDataStream output(&file1);
output << p2;
file.close();
output >> p2;
qDebug()<<p2.age<<p2.dBody.height<<p2.dBody.weight\
<<p2.vecfamily;//结果12 150.8 50 std::vector("sister", "mother", "father")
原文:https://www.cnblogs.com/that-boy-done/p/10739097.html
内容总结
以上是互联网集市为您收集整理的c++/qt的数据序列化和反序列化全部内容,希望文章能够帮你解决c++/qt的数据序列化和反序列化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。