C++ 序列化 serialization 如何将类持久化?
C++的类的持久化可以通过下面文章中所使用的方法来实现
其原理是将对象的内容以二进制的形式保存到文件中,
在要读取的时候再使用相反的过程来加载到对象中.
总结起来就是可以为要进行持久化的对象,比如说配置类,添加如下的两个方法:
bool Config::Save()
{
ofstream ofs("config.bin", ios::binary);
ofs.write((char *)this, sizeof(*this));
return true;
}
bool Config::Load()
{
ifstream ifs("config.bin", ios::binary);
ifs.read((char *)this, sizeof(*this));
return true;
}
参考文章:
Introduction
The C++ language provides a somewhat limited support for file processing. This is probably based on the time it was conceived and put to use. Many languages that were developed after C++, such as (Object) Pascal and Java provide a better support, probably because their libraries were implemented as the demand was made obvious. Based on this, C++ supports saving only values of primitive types such as short, int, char double. This can be done by using either the C FILE structure or C++' own fstream class.
Binary Serialization
Object serialization consists of saving the values that are part of an object, mostly the value gotten from declaring a variable of a class. AT the current standard, C++ doesn't inherently support object serialization. To perform this type of operation, you can use a technique known as binary serialization.
When you decide to save a value to a medium, the fstream class provides the option to save the value in binary format. This consists of saving each byte to the medium by aligning bytes in a contiguous manner, the same way the variables are stored in binary numbers.
To indicate that you want to save a value as binary, when declaring the ofstream variable, specify the ios option as binary. Here is an example:
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
return 0;
}
Writing to the Stream
The ios::binary option lets the compiler know how the value will be stored. This declaration also initiates the file. To write the values to a stream, you can call the fstream::write()method.
After calling the write() method, you can write the value of the variable to the medium. Here is an example:
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&one, sizeof(one));
return 0;
}
Reading From the Stream
Reading an object saved in binary format is as easy as writing it. To read the value, call the ifstream::read() method. Here is an example:
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
/* Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&one, sizeof(one));
*/
Student two;
ifstream ifs("fifthgrade.ros", ios::binary);
ifs.read((char *)&two, sizeof(two));
cout << "Student Information/n";
cout << "Student Name: " << two.FullName << endl;
cout << "Address: " << two.CompleteAddress << endl;
if( two.Gender == 'f' || two.Gender == 'F' )
cout << "Gender: Female" << endl;
else if( two.Gender == 'm' || two.Gender == 'M' )
cout << "Gender: Male" << endl;
else
cout << "Gender: Unknown" << endl;
cout << "Age: " << two.Age << endl;
if( two.LivesInASingleParentHome == true )
cout << "Lives in a single parent home" << endl;
else
cout << "Doesn't live in a single parent home" << endl;
cout << "/n";
return 0;
}
C++ 序列化 serialization 如何将类持久化?相关推荐
- [.net 面向对象程序设计进阶] (9) 序列化(Serialization) (一) 二进制流序列化
[.net 面向对象程序设计进阶] (9) 序列化(Serialization) (一) 二进制流序列化 本节导读: 在.NET编程中,经常面向对象处理完以后要转换成另一种格式传输或存储,这种将对 ...
- Boost 序列化 Serialization 底层实现
0.想写自己的二进制,不知道从何学起 为了写一套自己的二进制IO流,我翻了一遍boost的实现:这里讲输出binary部分 省流助手, 1.如果想实现基础类型的二进制流输出 去找 basi ...
- 【Java文件操作(七)】序列化:将自定义类写入文件
我的博客--Java文件操作系列 [Java文件操作(一)]递归打印文件目录 [Java文件操作(二)]删除文件夹,但保留其内部文件 [Java文件操作(三)]递归复制文件夹内所有文件 [Java文件 ...
- java安全编码指南之:序列化Serialization
文章目录 简介 序列化简介 注意serialVersionUID writeObject和readObject readResolve和writeReplace 不要序列化内部类 如果类中有自定义变量 ...
- Xml序列化、反序列化帮助类
之前从网络上找了一个Xml处理帮助类,并整理了一下,这个帮助类针对Object类型进行序列化和反序列化,而不需要提前定义Xml的结构,把它放在这儿供以后使用 1 /// <summary> ...
- Redis学习篇3_事务及其监控(锁)、Jedis、SpringBoot整合Redis、RedisTemplate的json序列化、RedisUtil工具类
目录 事务及其监控(锁) Jedis SpringBoot整合Redis RedisTemplate 默认RedisTemplate来源 关于中文序列化问题 RedisUtil工具类 一.事务及其监控 ...
- Java序列化(Serialization)的理解
2019独角兽企业重金招聘Python工程师标准>>> 1.什么是序列化 Java是面向对象的编程语言,有时需要保存对象,并在下次使用时可以顺利还原该对象.由于这种需求很常见,所以J ...
- 转:使用XMLSerializer类持久化数据
软在 .NET Framework 中通过System.Runtime.Serialization和System.Xml.Serialization提供了序列化功能,从这里我们可以看到微软已经承认了持 ...
- java oracle序列化_Java序列化(Serialization)的理解
1.什么是序列化 Java是面向对象的编程语言,有时需要保存对象,并在下次使用时可以顺利还原该对象.由于这种需求很常见,所以Java API对此提供了支持,添加相关程序代码到标准类库中,并将保存和还原 ...
最新文章
- 在visualstudio中使用Qt
- 如何做好错误处理?(PHP篇)
- matlab播放 视频帧,如何把连续视频帧转为视频的matlab代码 | 学步园
- tomcat7 加载el表达式 报错 使用tomcat8得以解决
- 《计算机应用基础》在线考试,2018年自学考试《计算机应用基础》考试题及答案...
- (转)uml 静态视图关系和关联
- 【BZOJ3156】防御准备,斜率优化DP
- 20200716:最多 K 次交换相邻数位后得到的最小整数(leetcode 1505)
- 敏捷落地的会议和工具
- iOS网络编程实践--蓝牙对等网络通信实例讲解
- Spring.net 中的AOP功能
- nekohtml的简单使用
- 精品微信小程序班级打卡系统+后台管理系统|前后分离VUE
- y = mapminmax(‘apply‘,x,ps)与mapminmax(‘reverse‘,y,ps)
- Opengl ES系列学习--点亮世界
- 软件测试教务系统测试用例,教务管理系统测试用例.doc
- HJ82 将真分数分解为埃及分数 —— 华为机考练习题
- Widows Git SSH
- Frequent values POJ - 3368(线段树)
- 深刻揭露当今楼市中的七大谬论