创新互联公司主营万山网站建设的网络公司,主营网站建设方案,app开发定制,万山h5成都微信小程序搭建,万山网站营销推广欢迎万山等地区企业咨询
1,关于序列化.png 2,关于序列化.png 3,关于序列化.png 将结构体写入文件 name存在空格,C++的序列化相当的脆弱 C++ 序列化 二进制版 C风格的 序列化,总是正常的 boost 序列化C风格 写入到文件simple boost 序列化C++风格 写入到文件simple boost 序列化从文件中读取 非侵入式的 boost序列化 缺点:所有的成员必须是public 可序列化的成员 派生类的序列化,问题引出,父类的成员增删对序列化有影响.耦合度太高 派生类的序列化 问题解决 指针的序列化 两个指针指向同一片区域,序列化到文件,只保存一份 智能指针的序列化 数组的序列化 指针数组的序列化 STL容器的序列化 将对象序列化为XML描述
将结构体写入文件
chunli@Linux:~/boost$ cat main.cpp #include#include using namespace std; struct User { string name; string email; int age; }; ostream& operator<<(ostream& os,const User& user ) { //空格分开,方便stream的读入 return os< >(istream& is,User& user ) { return is>>user.name>>user.email>>user.age; } int main() { User user1 = {"chunli","li_chunli@mail.com",24}; User user2 = {"林冲","林冲@email.com",30}; ofstream fout("user.txt");//为了演示,省略检查机制 fout<< user1 < > user3; fin >> user4; cout << user3 << endl; cout << user4 << endl; return 0; } chunli@Linux:~/boost$ > user.txt chunli@Linux:~/boost$ g++ main.cpp && ./a.out chunli li_chunli@mail.com 24 林冲 林冲@email.com 30 chunli@Linux:~/boost$ cat user.txt chunli li_chunli@mail.com 24 林冲 林冲@email.com 30 chunli@Linux:~/boost$
name存在空格,C++的序列化相当的脆弱
chunli@Linux:~/boost$ cat main.cpp #include#include using namespace std; struct User { string name; string email; int age; }; ostream& operator<<(ostream& os,const User& user ) { //空格分开,方便stream的读入 return os< >(istream& is,User& user ) { return is>>user.name>>user.email>>user.age; } int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林冲","林冲@email.com",30}; ofstream fout("user.txt");//为了演示,省略检查机制 fout<< user1 < > user3; fin >> user4; cout << user3 << endl; cout << user4 << endl; return 0; } chunli@Linux:~/boost$ >user.txt chunli@Linux:~/boost$ g++ main.cpp && ./a.out li chunli 0 335933440 chunli@Linux:~/boost$ cat user.txt li chunli li_chunli@mail.com 24 林冲 林冲@email.com 30 chunli@Linux:~/boost$
C++ 序列化 二进制版
chunli@Linux:~/boost$ cat main.cpp #include#include using namespace std; struct User { string name; string email; int age; }; int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林冲","林冲@email.com",30}; ofstream fout("user.bin");//为了演示,省略检查机制 fout.write((const char*)&user1,sizeof(struct User)); fout.write((const char*)&user2,sizeof(struct User)); fout.close(); User user3; User user4; ifstream fin("user.bin"); fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," < C风格的 序列化,总是正常的
chunli@Linux:~/boost$ cat main.cpp #include#include using namespace std; struct User { char name[100]; char email[100]; int age; }; int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林冲","林冲@email.com",30}; ofstream fout("user.bin");//为了演示,省略检查机制 fout.write((const char*)&user1,sizeof(struct User)); fout.write((const char*)&user2,sizeof(struct User)); fout.close(); User user3; ifstream fin("user.bin"); fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," < boost 序列化C风格 写入到文件simple
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include using namespace std; struct User { char name[16]; char email[64]; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; ofstream fout("user_info");//为了演示,省略检查机制 boost::archive::text_oarchive oa(fout); oa << user1; oa << user2; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 16 -26 -103 -70 -27 -92 -102 -26 -104 -97 0 0 0 0 0 0 0 64 -27 -112 -76 -25 -108 -88 64 101 109 97 105 108 46 99 111 109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40 16 -24 -79 -71 -27 -83 -112 -27 -92 -76 0 0 0 0 0 0 0 64 -26 -98 -105 -27 -122 -78 64 101 109 97 105 108 46 99 111 109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 chunli@Linux:~/boost$ boost 序列化C++风格 写入到文件simple
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; ofstream fout("user_info");//为了演示,省略检查机制 boost::archive::text_oarchive oa(fout); oa << user1; oa << user2; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吴用@email.com 40 9 豹子头 16 林冲@email.com 30 chunli@Linux:~/boost$ boost 序列化从文件中读取
chunli@Linux:~/boost$ >user_info chunli@Linux:~/boost$ cat main.cpp #include#include #include #include using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; ofstream fout("user_info");//为了演示,省略检查机制 boost::archive::text_oarchive oa(fout); oa << user1 << user2; fout.close(); User user_tmp; ifstream fin("user_info"); boost::archive::text_iarchive ia(fin); ia>>user_tmp; cout < >user_tmp; cout < 非侵入式的 boost序列化 缺点:所有的成员必须是public
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include using namespace std; struct User { string name; string email; int age; }; template void serialize(Archive &ar,User &user ,const unsigned version) { ar & user.name & user.email & user.age; } int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; ofstream fout("user_info");//为了演示,省略检查机制 boost::archive::text_oarchive oa(fout); oa << user1 << user2; fout.close(); User user_tmp; ifstream fin("user_info"); boost::archive::text_iarchive ia(fin); ia>>user_tmp; cout < >user_tmp; cout < 可序列化的成员
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct Group { string gid; User leader; vector members; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & gid & leader & members; } }; ostream& operator<<(ostream& os,const User& user) { return os< > group2; cout << group2.leader ; //copy(group2.members.begin(),group2.members.end(),ostream_iterator (cout," OK")); copy(group2.members.begin(),group2.members.end(),ostream_iterator (cout)); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星, 吴用@email.com, 40 豹子头, 林冲@email.com, 30 及时雨, 宋江@email.com, 42 黑旋风, 李逵@email.com, 37 chunli@Linux:~/boost$ cat group_info 22 serialization::archive 14 0 0 4 8080 0 0 9 智多星 16 吴用@email.com 40 0 0 3 0 9 豹子头 16 林冲@email.com 30 9 及时雨 16 宋江@email.com 42 9 黑旋风 16 李逵@email.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$ 派生类的序列化,问题引出,父类的成员增删对序列化有影响.耦合度太高
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct VIP:public User { int level; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & email;//将成员序列化到IO ar & level; ar & name; } }; int main() { VIP vip1; vip1.level = 100; vip1.name = "黑旋风"; vip1.email = "likui@mail.com"; ofstream fout("users_vip"); boost::archive::text_oarchive oa(fout); oa << vip1; fout.close(); VIP vip2; ifstream fin("users_vip"); boost::archive::text_iarchive ia(fin); ia >> vip2; cout << vip2.level << endl; cout << vip2.name << endl; cout << vip2.email << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 100 黑旋风 likui@mail.com chunli@Linux:~/boost$ cat users_vip 22 serialization::archive 14 0 0 14 likui@mail.com 100 9 黑旋风chunli@Linux:~/boost$ chunli@Linux:~/boost$
派生类的序列化 问题解决
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct VIP:public User { int level; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & boost::serialization::base_object (*this);//将父类序列化,不再管父类有多少个成员 ar & level; } }; int main() { VIP vip1; vip1.level = 100; vip1.name = "黑旋风"; vip1.age = 34; vip1.email = "likui@mail.com"; ofstream fout("users_vip"); boost::archive::text_oarchive oa(fout); oa << vip1; fout.close(); VIP vip2; ifstream fin("users_vip"); boost::archive::text_iarchive ia(fin); ia >> vip2; cout << vip2.level << endl; cout << vip2.name << endl; cout << vip2.email << endl; cout << vip2.age << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 100 黑旋风 likui@mail.com 34 chunli@Linux:~/boost$ cat users_vip 22 serialization::archive 14 0 0 0 0 9 黑旋风 14 likui@mail.com 34 100chunli@Linux:~/boost$ chunli@Linux:~/boost$ 指针的序列化
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及时雨","songjiang@mail.com",37); ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; file.close(); delete user1; User *user2; ifstream fin("user.info"); //boost::archive::text_iarchive ia(fin); boost::archive::text_iarchive ia(fin); ia >> user2; cout <<"name="< getName()<<", email="< getEmail()<<", age="<< user2->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及时雨, email=songjiang@mail.com, age=37 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及时雨 18 songjiang@mail.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
两个指针指向同一片区域,序列化到文件,只保存一份
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及时雨","songjiang@mail.com",37); User *user2 = user1; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; oa << user2; file.close(); delete user1; User *user3; User *user4; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> user3; cout <<"name="< getName()<<", email="< getEmail()<<", age="<< user3->getAge()<< endl; ia >> user4; cout <<"name="< getName()<<", email="< getEmail()<<", age="<< user4->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及时雨, email=songjiang@mail.com, age=37 name=及时雨, email=songjiang@mail.com, age=37 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及时雨 18 songjiang@mail.com 37 0 0chunli@Linux:~/boost$ chunli@Linux:~/boost$ 问题:以指针序列化,以对象读取,失败 智能指针的序列化
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include #include using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及时雨","songjiang@mail.com",37); User *user2 = user1; boost::shared_ptr sp1(new User("花和尚", "鲁智深@mail.com", 38)); boost::shared_ptr sp2 = sp1; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; oa << user2; oa << sp1; oa << sp2; file.close(); delete user1; User *user3; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> user3; cout <<"name="< getName()<<", email="< getEmail()<<", age="<< user3->getAge()<< endl; ia >> user3; cout <<"name="< getName()<<", email="< getEmail()<<", age="<< user3->getAge()<< endl; boost::shared_ptr sp3; ia >> sp3; cout <<"name="< getName()<<", email="< getEmail()<<", age="<< sp3->getAge()<< endl; ia >> sp3; cout <<"name="< getName()<<", email="< getEmail()<<", age="<< sp3->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及时雨, email=songjiang@mail.com, age=37 name=及时雨, email=songjiang@mail.com, age=37 name=花和尚, email=鲁智深@mail.com, age=38 name=花和尚, email=鲁智深@mail.com, age=38 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及时雨 18 songjiang@mail.com 37 0 0 0 1 0 1 9 花和尚 18 鲁智深@mail.com 38 0 1chunli@Linux:~/boost$ chunli@Linux:~/boost$
数组的序列化
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include #include using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User users[3] = { User("及时雨","songjiang@mail.com",37), User("武行者","wusong@mail.com",37), User("花和尚","luzhishen@mail.com",40) }; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << users; file.close(); User buf[3]; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> buf; cout <
指针数组的序列化
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include #include using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *users[3] = { new User("及时雨","songjiang@mail.com",37), new User("武行者","wusong@mail.com",37), new User("花和尚","luzhishen@mail.com",40) }; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << users; file.close(); User *buf[3]; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> buf; cout < getName()<<","< getEmail()<<","<< buf[0]->getAge()<< endl; cout < getName()<<","< getEmail()<<","<< buf[1]->getAge()<< endl; cout < getName()<<","< getEmail()<<","<< buf[2]->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out 及时雨,songjiang@mail.com,37 武行者,wusong@mail.com,37 花和尚,luzhishen@mail.com,40 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 3 0 1 0 0 9 及时雨 18 songjiang@mail.com 37 0 1 9 武行者 15 wusong@mail.com 37 0 2 9 花和尚 18 luzhishen@mail.com 40chunli@Linux:~/boost$ chunli@Linux:~/boost$
STL容器的序列化
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct Group { string gid; User leader; vector members; friend class boost::serialization::access; template void serialize(Archive &ar,const unsigned version) { ar & gid & leader & members; } }; ostream& operator<<(ostream& os,const User& user) { return os< > group2; cout << group2.leader ; //copy(group2.members.begin(),group2.members.end(),ostream_iterator (cout," OK")); copy(group2.members.begin(),group2.members.end(),ostream_iterator (cout)); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星, 吴用@email.com, 40 豹子头, 林冲@email.com, 30 及时雨, 宋江@email.com, 42 黑旋风, 李逵@email.com, 37 chunli@Linux:~/boost$ cat group_info 22 serialization::archive 14 0 0 4 8080 0 0 9 智多星 16 吴用@email.com 40 0 0 3 0 9 豹子头 16 林冲@email.com 30 9 及时雨 16 宋江@email.com 42 9 黑旋风 16 李逵@email.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
将对象序列化为XML描述
chunli@Linux:~/boost$ cat main.cpp #include#include #include #include #include #include #include #include using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template void serialize(Archive& ar, const unsigned version) { ar & BOOST_SERIALIZATION_NVP(name); ar & BOOST_SERIALIZATION_NVP(email); ar & BOOST_SERIALIZATION_NVP(age); } }; ostream& operator<<(ostream& os, const User& u) { return os << u.name << ' ' << u.email << ' ' << u.age; } struct Group { string gid; User leader; vector members; friend class boost::serialization::access; template void serialize(Archive& ar, const unsigned version) { ar & BOOST_SERIALIZATION_NVP(gid); ar & BOOST_SERIALIZATION_NVP(leader); ar & BOOST_SERIALIZATION_NVP(members); } }; int main() { Group group; group.gid = "G01"; User leader = { "tom", "tom@abc.com", 28 }; group.leader = leader; User u1 = { "tom1", "tom1@abc.com", 28 }; User u2 = { "tom2", "tom2@abc.com", 28 }; User u3 = { "tom3", "tom3@abc.com", 28 }; group.members.push_back(u1); group.members.push_back(u2); group.members.push_back(u3); ofstream fout("users_xml"); boost::archive::xml_oarchive oa(fout); oa << BOOST_SERIALIZATION_NVP(group); fout.close(); Group g2; ifstream fin("users_xml"); boost::archive::xml_iarchive ia(fin); ia >> BOOST_SERIALIZATION_NVP(g2); cout << g2.leader << endl; copy(g2.members.begin(), g2.members.end(), ostream_iterator (cout,"\n")); } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out tom tom@abc.com 28 tom1 tom1@abc.com 28 tom2 tom2@abc.com 28 tom3 tom3@abc.com 28 chunli@Linux:~/boost$ cat users_xml chunli@Linux:~/boost$ chunli@Linux:~/boost$ G01 tom tom@abc.com 28 3 0 tom1 tom1@abc.com 28 tom2 tom2@abc.com 28 tom3 tom3@abc.com 28
文章标题:7C++Boost序列化
链接URL:http://kswsj.cn/article/jjidpp.html