1 什么是序列化和反序列化
当我们在写程序时,比如说我们自定义了一个实体类
Person
,然后在程序中创建一个该实体类对象,并给对象赋了一些值,但是我们想将这些数据发给我们的其他的程序员朋友,让他们也可以调用我们创建的这个实体类并使用我们的数据,这个时候我们就需要使用到序列化和发序列化。
1.1 序列化
序列化就是将内存中运行的程序中的数据结构
数据存储到磁盘的文件
中,使其能够持久化存在电脑中,就算程序停止,这些数据也能一直存在。、
1.2 反序列化
顾名思义,也就是序列化的反操作,就是将已经存在磁盘文件中的数据读取到程序中继续使用的这么一个操作。
2 代码实现
#include <iostream>
#include "string.h"
#include <fstream>
using namespace std;
//定义了一个实体类 Person
class Person
{
public:
Person(char* name, int age)
{
strcpy(this->name, name);
this->age = age;
}
Person() {}
void showInfo()
{
cout << name << " " << age << endl;
}
int getAge()
{
return age;
}
char * getName()
{
return name;
}
public:
char name[10] { 0 };
int age = 0;
};
//二进制文件 进行写
void WriteBinaryFile(Person * ps,int size)
{
ofstream file("binary.txt",ios::out | ios::binary );
if (!file.is_open())
{
cout << "文件打开失败" << endl;
}
for (int i=0;i<size;i++)
{
file.write((char*)&ps[i],sizeof(Person));
}
file.close();
}
//二进制文件 进行读
Person * ReadBinaryFile()
{
ifstream file("binary.txt", ios::in | ios::binary);
if (!file.is_open())
{
cout << "文件打开失败" << endl;
}
//开辟一块空间 存放读取的数据
char* temp = new char[sizeof(Person)];
//或者 Person p;开辟的空间肯定合适
//Person temp;
Person* ps = new Person[5];
int i=0;
//将数据读入的 temp对应的空间
while (file.read(temp,sizeof(Person)))
{
Person p = *(Person*)temp;
ps[i] = p;
i++;
}
file.close();
return ps;
}
int main(int argc, char *argv[])
{
//序列化操作 往文件中写数据
// Person* ps = new Person[5];
// ps[0] = Person("Lay1", 11);
// ps[1] = Person("Lay1", 12);
// ps[3] = Person("Lay1", 13);
// ps[2] = Person("Lay1", 14);
// ps[4] = Person("Lay1", 15);
// WriteBinaryFile(ps,5);
//反序列化操作 程序读取文件中的数据
Person* ps2;
ps2 = ReadBinaryFile();
for (int i=0; i<5; i++)
{
cout << ps2[i].getName() << " " << ps2[i].getAge() << endl;
}
return 0;
}
存储在文件中的内容的样子:
运行结果:
写在最后
欢迎大家关注鄙人的公众号【麦田里的守望者zhg】,让我们一起成长,谢谢。我的博客