日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ 序列化 serialization 如何将类持久化?

發布時間:2023/12/10 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 如何将类持久化?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。