电子科技大学实验报告学生姓名:学号:指导教师:实验地点:实验时间:2011.12.14一、实验室名称:Linux环境高级编程实验室二、实验项目名称:对象序列化实验三、实验学时:8学时四、实验目的:熟悉基本的对象序列化方法五、实验内容:共进行5个版本的开发:版本1:将一个类的一个对象序列化到文件版本2:将一个类的多个对象序列化到文件版本3:将两个类的多个对象序列化到文件版本4:按照面向对象的方法,解决多个类的多个对象序列化到文件的问题版本5:序列化的目的地不仅可以是文件,还可以是其他,即可配置性六、实验步骤:实验一:Test_1.cpp:#includefcntl.h#includevector#includeiostreamusingnamespacestd;//指定名字空间classtest_1{private:intx;public:test_1(){intx=0;}explicittest_1(inty){x=y;}virtual~test_1()//虚函数{}public:voidfile(){coutinfile():xendl;}public:boolSerialize(constchar*path)const//序列化部分{intfd=open(path,O_RDWR|O_CREAT|O_TRUNC,0);//打开experiment文件if(-1==fd)returnfalse;if(write(fd,&x,sizeof(int))==-1)//写文件{close(fd);returnfalse;}if(::close(fd)==-1)//关闭文件returnfalse;returntrue;}boolDeserialize(constchar*path)//反序列化部分{intfd=open(path,O_RDWR);//if(-1==fd)returnfalse;intred=read(fd,&x,sizeof(int));//只序列化一个值if(-1==red){close(fd);returnfalse;}if(close(fd)==-1)returnfalse;returntrue;}};intmain(){{test_1ex(1314);ex.Serialize(recored.txt);}{test_1ex;ex.Deserialize(recored.txt);ex.file();}return0;}运行结果:如图1所示图1:test_1运行结果实验二:Test_2.cpp:#includefcntl.h#includevector#includeiostreamusingnamespacestd;classtest_2{private:intx;public:test_2(){intx=0;}explicittest_2(inty){x=y;}virtual~test_2(){}public:voidfile(){coutinfile():xendl;//显示x值}public:boolSerialize(constchar*Path)const{intfd=open(Path,O_RDWR|O_CREAT|O_TRUNC,0);//打开文件if(-1==fd)returnfalse;if(Serialize(fd)==false)//函数重载{close(fd);returnfalse;}if(close(fd)==-1)returnfalse;returntrue;}boolDeserialize(constchar*Path){intfd=open(Path,O_RDWR);if(-1==fd)returnfalse;if(Deserialize(fd)==false){close(fd);returnfalse;}if(close(fd)==-1)returnfalse;returntrue;}boolSerialize(intfd)const{if(-1==fd)returnfalse;if(write(fd,&x,sizeof(int))==-1)//x值写入文件returnfalse;returntrue;}boolDeserialize(intfd){if(-1==fd)returnfalse;intrd=read(fd,&x,sizeof(int));//读出文件中的x值if((0==rd)||(-1==rd))returnfalse;returntrue;}};classSerializerFortest_2{public:SerializerFortest_2(){}virtual~SerializerFortest_2(){}public:boolSerialize(constchar*Path,constvectortest_2&vec){intfd=open(Path,O_RDWR|O_CREAT|O_APPEND,0);//打开文件if(-1==fd)returnfalse;for(intx=0;xvec.size();x++)//写入数组{vec[x].Serialize(fd);}close(fd);returntrue;}boolDeserialize(constchar*Path,vectortest_2&vec){intfd=open(Path,O_RDWR);if(-1==fd)returnfalse;for(;;){test_2ex;if(ex.Deserialize(fd)==true)//读出数组{vec.push_back(ex);}elsebreak;}close(fd);returntrue;}};intmain(){{test_2ex1(6),ex2(7),ex3(9);//序列化数组vectortest_2vec;vec.push_back(ex1);vec.push_back(ex2);vec.push_back(ex3);SerializerFortest_2ser;ser.Serialize(record.txt,vec);}{SerializerFortest_2ser;//反序列化vectortest_2vec;ser.Deserialize(record.txt,vec);for(intx=0;x3;x++){vec[x].file();}}return0;}运行结果如图2所示:图2:test_2运行结果实验三:Test_3.cpp:#includefcntl.h#includevector#includeiostreamusingnamespacestd;classtest_A{private:intx;public:test_A(){intx=0;}explicittest_A(inty){x=y;}virtual~test_A(){}public:voidfile(){coutinfile():xendl;}public:boolSerialize(intfd){if(-1==fd)returnfalse;if(::write(fd,&x,sizeof(int))==-1)returnfalse;returntrue;}boolDeserialize(intfd){if(-1==fd)returnfalse;intrd=read(fd,&x,sizeof(int));if((0==rd)||(-1==rd))returnfalse;returntrue;}};classtest_B{private:intx;inty;public:test_B(){x=0;y=0;}explicittest_B(intk){x=k;y=k+1;}virtual~test_B(){}public:voidfile(){coutinfile():xyendl;}public:boolSerialize(intfd){if(-1==fd)returnfalse;if(write(fd,&x,sizeof(int))==-1)returnfalse;if(write(fd,&y,sizeof(int))==-1)returnfalse;returntrue;}boolDeserialize(intfd){if(-1==fd)returnfalse;intrd=read(fd,&x,sizeof(int));if((0==rd)||(-1==rd))returnfalse;rd=read(fd,&y,sizeof(int));if((0==rd)||(-1==rd))returnfalse;returntrue;}};structSerialized{intnType;//0fortest_A;1fortest_Bvoid*pObj;};classSerializer{public:boolSerialize(constchar*Path,std::vectorSerialized&vec){intfd=open(Path,O_RDWR|O_CREAT|O_TRUNC,0);if(-1==fd)returnfalse;for(intx=0;xvec.size();x++){if(write(fd,&(vec[x].nType),4)==-1){close(fd);returnfalse;}if(0==vec[x].nType){test_A*p=(test_A*)(vec[x].pObj);if(p-Serialize(fd)==false)returnfalse;}elseif(1==vec[x].nType){test_B*p=(test_B*)(vec[x].pObj);if(p-Serialize(fd)==false)returnfalse;}}if(close(fd)==-1)returnfalse;returntrue;}boolDeserialize(constchar*Path,std::vectorSerialized&vec){intfd=open(Path,O_RDWR);if(-1==fd)returnfalse;for(;;){intnType;intrd=read(fd,&nType,4);if((-1==rd)||(0==rd))break;if(0==nType){test_A*p;p=newtest_A();p-Deserialize(fd);Serializeds;s.nType=nType;s.pObj=p;vec.push_back(s);}elseif(1==nType){test_B*p;p=newtest_B();p-Deserialize(fd);Serializeds;s.nType=nType;s.pObj=p;vec.push_back(s);}}if(close(fd)==-1)returnfalse;returntrue;}};intmain(){{test_Aex1(2);Serializeds1;s1.nType=0;s1.pObj=&ex1;test_Bb1(3);Serializeds2;s2.nType=1;s2.pObj=&b1;test_Bb2(4);Serializeds3;s3.nType=1;s3.pObj=&b2;test_Aex2(5);Serializeds4;s4.nType=0;s4.pObj=&ex2;std::vectorSerializedvec;vec.push_back(s1);vec.push_back(s2);vec.push_back(s3);vec.push_back(s4);Serializers;s.Serialize(data,vec);}{Serializers;std: