1、对象序列化实验电 子 科 技 大 学实 验 报 告学生姓名: 学 号: 指导教师:实验地点: 实验时间:2011.12.14一、实验室名称:Linux环境高级编程实验室二、实验项目名称:对象序列化实验三、实验学时:8学时四、实验目的:熟悉基本的对象序列化方法5、实验内容:共进行5个版本的开发: 版本1:将一个类的一个对象序列化到文件 版本2:将一个类的多个对象序列化到文件 版本3:将两个类的多个对象序列化到文件 版本4:按照面向对象的方法,解决多个类的多个对象序列化到文件的问题 版本5:序列化的目的地不仅可以是文件,还可以是其他,即可配置性六、实验步骤:实验一 :Test_1.cpp:#inc
2、lude #include #include using namespace std; /指定名字空间class test_1private: int x;public: test_1() int x = 0; explicit test_1(int y) x = y; virtual test_1() /虚函数 public: void file() cout in file(): x endl; public: bool Serialize(const char *path) const /序列化部分 int fd = open(path, O_RDWR | O_CREAT | O_TRU
3、NC, 0); /打开experiment文件 if(-1 = fd) return false; if(write(fd, &x, sizeof(int) = -1)/写文件 close(fd); return false; if(:close(fd) = -1)/关闭文件 return false; return true; bool Deserialize(const char *path) /反序列化部分 int fd = open(path, O_RDWR);/ if(-1 = fd) return false; int red = read(fd, &x, sizeof(int);
4、/只序列化一个值 if(-1 = red) close(fd); return false; if(close(fd) = -1) return false; return true; ;int main() test_1 ex(1314); ex.Serialize(recored.txt); test_1 ex; ex.Deserialize(recored.txt); ex.file(); return 0;运行结果:如图1所示图1:test_1运行结果实验二:Test_2.cpp:#include #include #include using namespace std;class
5、test_2private: int x;public: test_2() int x = 0; explicit test_2(int y) x = y; virtual test_2() public: void file() cout in file(): x endl;/显示x值 public: bool Serialize(const char *Path) const int fd = open(Path, O_RDWR | O_CREAT | O_TRUNC, 0);/打开文件 if(-1 = fd) return false; if(Serialize(fd) = false)
6、/函数重载 close(fd); return false; if(close(fd) = -1) return false; return true; bool Deserialize(const char *Path) int fd = open(Path, O_RDWR); if(-1 = fd) return false; if(Deserialize(fd) = false) close(fd); return false; if(close(fd) = -1) return false; return true; bool Serialize(int fd) const if(-1
7、 = fd) return false; if(write(fd, &x, sizeof(int) = -1)/x值写入文件 return false; return true; bool Deserialize(int fd) if(-1 = fd) return false; int rd = read(fd, &x, sizeof(int);/读出文件中的x值 if(0 = rd) | (-1 = rd) return false; return true; ;class SerializerFortest_2public: SerializerFortest_2() virtual S
8、erializerFortest_2() public: bool Serialize(const char *Path, const vector& vec) int fd = open(Path, O_RDWR | O_CREAT | O_APPEND, 0);/打开文件 if(-1 = fd) return false; for(int x= 0; x vec.size(); x+)/写入数组 vecx.Serialize(fd); close(fd); return true; bool Deserialize(const char *Path, vector& vec) int fd
9、 = open(Path, O_RDWR); if(-1 = fd) return false; for(;) test_2 ex; if(ex.Deserialize(fd) = true)/读出数组 vec.push_back(ex); else break; close(fd); return true; ;int main() test_2 ex1(6), ex2(7), ex3(9);/序列化数组 vector vec; vec.push_back(ex1); vec.push_back(ex2); vec.push_back(ex3); SerializerFortest_2 se
10、r; ser.Serialize(record.txt, vec); SerializerFortest_2 ser;/反序列化 vector vec; ser.Deserialize(record.txt, vec); for(int x = 0; x 3; x+) vecx.file(); return 0;运行结果如图2所示:图2:test_2运行结果实验三:Test_3.cpp:#include #include #include using namespace std;class test_Aprivate: int x;public: test_A() int x = 0; exp
11、licit test_A(int y) x = y; virtual test_A() public: void file() coutin file(): xendl; public: bool Serialize(int fd) if(-1 = fd) return false; if(:write(fd, &x, sizeof(int) = -1) return false; return true; bool Deserialize(int fd) if(-1 = fd) return false; int rd = read(fd, &x, sizeof(int); if(0 = r
12、d) | (-1 = rd) return false; return true; ;class test_Bprivate: int x; int y;public: test_B() x = 0; y = 0; explicit test_B(int k) x = k; y = k + 1; virtual test_B() public: void file() coutin file(): x y endl; public: bool Serialize(int fd) if(-1 = fd) return false; if(write(fd, &x, sizeof(int) = -
13、1) return false; if(write(fd, &y, sizeof(int) = -1) return false; return true; bool Deserialize(int fd) if(-1 = fd) return false; int rd = read(fd, &x, sizeof(int); if(0 = rd) | (-1 = rd) return false; rd = read(fd, &y, sizeof(int); if(0 = rd) | (-1 = rd) return false; return true; ;struct Serialize
14、d int nType; /0 for test_A; 1 for test_B void *pObj;class Serializerpublic: bool Serialize(const char *Path,std:vector &vec) int fd = open(Path, O_RDWR | O_CREAT | O_TRUNC, 0); if(-1 = fd) return false; for(int x = 0; x Serialize(fd) = false) return false; else if(1 = vecx.nType) test_B *p = (test_B
15、 *)(vecx.pObj); if(p-Serialize(fd) = false) return false; if(close(fd) = -1) return false; return true; bool Deserialize(const char *Path,std:vector& vec) int fd = open(Path, O_RDWR); if(-1 = fd) return false; for(;) int nType; int rd = read(fd, &nType, 4); if(-1 = rd) | (0 = rd) break; if(0 = nType
16、) test_A *p; p = new test_A(); p-Deserialize(fd); Serialized s; s.nType = nType; s.pObj = p; vec.push_back(s); else if(1 = nType) test_B *p; p = new test_B(); p-Deserialize(fd); Serialized s; s.nType = nType; s.pObj = p; vec.push_back(s); if(close(fd) = -1) return false; return true; ;int main() tes
17、t_A ex1(2); Serialized s1; s1.nType = 0; s1.pObj = &ex1; test_B b1(3); Serialized s2; s2.nType = 1; s2.pObj = &b1; test_B b2(4); Serialized s3; s3.nType = 1; s3.pObj = &b2; test_A ex2(5); Serialized s4; s4.nType = 0; s4.pObj = &ex2; std:vector vec; vec.push_back(s1); vec.push_back(s2); vec.push_back
18、(s3); vec.push_back(s4); Serializer s; s.Serialize(data, vec); Serializer s; std:vector vec; s.Deserialize(data, vec); for(int x = 0; x file(); else if(vecx.nType = 1) test_B *p = (test_B *)(vecx.pObj); p-file(); return 0;运行结果如图3所示:图3:test_3运行结果实验四:Test_4.cpp:#include #include #include using namespa
19、ce std;class testSerializable/序列化虚类public: virtual bool Serialize(int fd) = 0; virtual testSerializable* Deserialize(int fd) = 0; virtual bool GetType(int& type) = 0;public: testSerializable() virtual testSerializable() ;class test_1 : public testSerializable private: int i;public: test_1() i = 0; e
20、xplicit test_1(int j) i = j; virtual test_1() public: void file() std:cout in file(): i i), sizeof(int); if(0 = r) | (-1 = r) delete p; return NULL; return p; ;class test_2 : public testSerializableprivate: int i; int j;public: test_2() i = 0; j = 0; explicit test_2(int k) i = k; j = k + 1; virtual
21、test_2() public: void file() std:cout in file(): i j i), sizeof(int); if(0 = r) | (-1 = r) delete p; return NULL; r = :read(fd, &(p-j), sizeof(int); if(0 = r) | (-1 = r) delete p; return NULL; return p; ;class CLSerializerprivate: std:vector m_vSerialized;public: bool Serialize(const char *pFilePath, std:vector& v) int fd = :open(pFilePath, O_RDWR | O_CREAT | O_TRUNC, 0); if(-1 = fd) return false; for(int i = 0; i GetType(type); if(:write(fd, &type, 4) = -1) :close(fd); return false; vi-Serialize(fd); if(:close(fd) = -1) return false; r
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1