ImageVerifierCode 换一换
格式:DOCX , 页数:37 ,大小:147.68KB ,
资源ID:9927753      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9927753.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(对象序列化实验.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

对象序列化实验.docx

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