




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上電 子 科 技 大 學(xué)實 驗 報 告學(xué)生姓名: 學(xué) 號: 指導(dǎo)教師:實驗地點: 實驗時間:2011.12.14一、實驗室名稱:Linux環(huán)境高級編程實驗室二、實驗項目名稱:對象序列化實驗三、實驗學(xué)時:8學(xué)時四、實驗?zāi)康模菏煜せ镜膶ο笮蛄谢椒?、 實驗內(nèi)容:共進(jìn)行5個版本的開發(fā):l 版本1:將一個類的一個對象序列化到文件l 版本2:將一個類的多個對象序列化到文件l 版本3:將兩個類的多個對象序列化到文件l 版本4:按照面向?qū)ο蟮姆椒?,解決多個類的多個對象序列化到文件的問題l 版本5:序列化的目的地不僅可以是文件,還可以是其他,即可配置性六、實驗步驟:實驗一 :Tes
2、t_1.cpp:#include <fcntl.h>#include <vector>#include <iostream>using namespace std; /指定名字空間class test_1private:int x;public: test_1() int x = 0; explicit test_1(int y) x = y; virtual test_1() /虛函數(shù) public: void file() cout << "in file(): " << x << endl; pu
3、blic: bool Serialize(const char *path) const /序列化部分 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0); /打開experiment文件if(-1 = fd)return false;if(write(fd, &x, sizeof(int) = -1)/寫文件close(fd);return false;if(:close(fd) = -1)/關(guān)閉文件return false;return true; bool Deserialize(const char *path) /反序列化部分
4、 int fd = open(path, O_RDWR);/if(-1 = fd)return false;int red = read(fd, &x, sizeof(int);/只序列化一個值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.fi
5、le(); return 0;運(yùn)行結(jié)果:如圖1所示圖1:test_1運(yùn)行結(jié)果實驗二:Test_2.cpp:#include <fcntl.h>#include <vector>#include <iostream>using namespace std;class 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(): &q
6、uot; << 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)/函數(shù)重載close(fd);return false;if(close(fd) = -1)return false;return true; bool Deserialize(const char *Path)int fd
7、 = 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 = fd)return false;if(write(fd, &x, sizeof(int) = -1)/x值寫入文件return false;return true; bool Deserialize(int fd) if(-1 = fd)
8、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 SerializerFortest_2() public: bool Serialize(const char *Path, const vector<test_2>& vec) int fd = open(Path, O_RDWR |
9、 O_CREAT | O_APPEND, 0);/打開文件if(-1 = fd)return false;for(int x= 0; x < vec.size(); x+)/寫入數(shù)組vecx.Serialize(fd);close(fd);return true; bool Deserialize(const char *Path, vector<test_2>& vec) int fd = open(Path, O_RDWR);if(-1 = fd)return false;for(;)test_2 ex;if(ex.Deserialize(fd) = true)/
10、讀出數(shù)組vec.push_back(ex);elsebreak;close(fd);return true;int main() test_2 ex1(6), ex2(7), ex3(9);/序列化數(shù)組vector<test_2> vec;vec.push_back(ex1);vec.push_back(ex2);vec.push_back(ex3);SerializerFortest_2 ser;ser.Serialize("record.txt", vec); SerializerFortest_2 ser;/反序列化vector<test_2>
11、 vec;ser.Deserialize("record.txt", vec);for(int x = 0; x < 3; x+)vecx.file(); return 0;運(yùn)行結(jié)果如圖2所示:圖2:test_2運(yùn)行結(jié)果實驗三:Test_3.cpp:#include <fcntl.h>#include <vector>#include <iostream>using namespace std;class test_Aprivate: int x;public: test_A() int x = 0; explicit test_A
12、(int y) x = y; virtual test_A() public: void file() cout<<"in file(): "<<x<<endl; 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, &am
13、p;x, sizeof(int);if(0 = rd) | (-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() cout<<"in file(): "<<x<<" "<<y<< endl; publ
14、ic: bool Serialize(int fd) if(-1 = fd)return false;if(write(fd, &x, sizeof(int) = -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
15、, &y, sizeof(int);if(0 = rd) | (-1 = rd)return false;return true; ;struct Serialized int nType; /0 for test_A; 1 for test_B void *pObj;class Serializerpublic: bool Serialize(const char *Path,std:vector<Serialized> &vec) int fd = open(Path, O_RDWR | O_CREAT | O_TRUNC, 0);if(-1 = fd)retu
16、rn false;for(int x = 0; x < vec.size(); x+)if(write(fd, &(vecx.nType), 4) = -1)close(fd);return false;if(0 = vecx.nType)test_A *p = (test_A *)(vecx.pObj);if(p->Serialize(fd) = false)return false;else if(1 = vecx.nType)test_B *p = (test_B *)(vecx.pObj);if(p->Serialize(fd) = false)return
17、false;if(close(fd) = -1)return false;return true; bool Deserialize(const char *Path,std:vector<Serialized>& 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)test_A *p;p = new test_A();p->De
18、serialize(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() test_A ex1(2);Serialized s1;s1.nType = 0;s1.pObj =
19、&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<Serialized> vec;vec.push_back(s1);vec.push_back(s2);vec.push_back(s3);vec.push_back(s4);Serializer
20、 s;s.Serialize("data", vec); Serializer s;std:vector<Serialized> vec;s.Deserialize("data", vec);for(int x = 0; x < vec.size(); x+)if(vecx.nType = 0)test_A *p = (test_A *)(vecx.pObj);p->file();else if(vecx.nType = 1)test_B *p = (test_B *)(vecx.pObj);p->file();return
21、 0;運(yùn)行結(jié)果如圖3所示:圖3:test_3運(yùn)行結(jié)果實驗四:Test_4.cpp:#include <fcntl.h>#include <iostream>#include <vector>using namespace std;class testSerializable/序列化虛類public:virtual bool Serialize(int fd) = 0; virtual testSerializable* Deserialize(int fd) = 0; virtual bool GetType(int& type) = 0;publi
22、c: testSerializable() virtual testSerializable() ;class test_1 : public testSerializableprivate: int i;public: test_1() i = 0; explicit test_1(int j) i = j; virtual test_1() public: void file() std:cout << "in file(): " << i << std:endl; public: virtual bool GetType(int&a
23、mp; type) type = 0;return true; virtual bool Serialize(int fd) if(-1 = fd)return false;if(:write(fd, &i, sizeof(int) = -1)/序列化return false;return true; virtual testSerializable* Deserialize(int fd)/反序列化 if(-1 = fd)return NULL;test_1 *p = new test_1();int r = :read(fd, &(p->i), sizeof(int)
24、;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 test_2() public: void file() std:cout << "in file(): " << i << " " <
25、< j << std:endl; public: virtual bool GetType(int& type) type = 1;return true; virtual bool Serialize(int fd)/寫入多個對象 if(-1 = fd)return false;if(:write(fd, &i, sizeof(int) = -1)return false;if(:write(fd, &j, sizeof(int) = -1)return false;return true; virtual testSerializable* Des
26、erialize(int fd) if(-1 = fd)return NULL;test_2 *p = new test_2();int r = :read(fd, &(p->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<testSerializa
27、ble*> m_vSerialized;public: bool Serialize(const char *pFilePath, std:vector<testSerializable*>& v) int fd = :open(pFilePath, O_RDWR | O_CREAT | O_TRUNC, 0);if(-1 = fd)return false;for(int i = 0; i < v.size(); i+)int type;vi->GetType(type);if(:write(fd, &type, 4) = -1):close(f
28、d);return false;vi->Serialize(fd);if(:close(fd) = -1)return false;return true;bool Deserialize(const char *pFilePath, std:vector<testSerializable*>& v)int fd = :open(pFilePath, O_RDWR);if(-1 = fd) return false;for(;) int nType; int r = :read(fd, &nType, 4); if(-1 = r) | (0 = r)break
29、; int type; for(int i = 0; i < m_vSerialized.size(); i+) m_vSerializedi->GetType(type);if(type = nType) testSerializable *p = m_vSerializedi->Deserialize(fd); if(p != NULL)v.push_back(p); if(:close(fd) = -1) return false;return true; void Register(testSerializable *pSerialized) m_vSerialize
30、d.push_back(pSerialized); ;int main() test_1 ex1(2);test_2 e1(3);test_2 e2(4);test_1 ex2(5);std:vector<testSerializable*> v;v.push_back(&ex1);v.push_back(&e1);v.push_back(&e2);v.push_back(&ex2); CLSerializer s;s.Serialize("data", v); CLSerializer s;test_1 a;test_2 b;s
31、.Register(&a);s.Register(&b);std:vector<testSerializable*> v;s.Deserialize("data", v);for(int i = 0; i < v.size(); i+)test_1 *p = dynamic_cast<test_1 *>(vi);if(p != NULL)p->file();test_2 *q = dynamic_cast<test_2 *>(vi);if(q != NULL)q->file();return 0;運(yùn)行結(jié)果
32、如圖4所示:圖4:test_4運(yùn)行結(jié)果實驗五:Test_5.cpp:#include <fcntl.h>#include <iostream>#include <vector>#include <strings.h>#include <stdio.h>class ILSerializable/虛類public: virtual bool Serialize(int fd) = 0; virtual ILSerializable* Deserialize(int fd) = 0; virtual bool GetType(int&
33、; type) = 0;public: ILSerializable() virtual ILSerializable() ;class A : public ILSerializableprivate: int i;public: A() i = 0; explicit A(int j) i = j; virtual A() public: void f() std:cout << "in f(): " << i << std:endl; public: virtual bool GetType(int& type) type
34、= 0;return true; virtual bool Serialize(int fd)/文件可配置 if(-1 = fd)return false;if(:write(fd, &i, sizeof(int) = -1)return false;return true; virtual ILSerializable* Deserialize(int fd) if(-1 = fd)return NULL;A *p = new A();int r = :read(fd, &(p->i), sizeof(int);/此處反序列化if(0 = r) | (-1 = r)de
35、lete p;return NULL;return p;class B : public ILSerializableprivate: int i; int j;public: B() i = 0;j = 0; explicit B(int k) i = k;j = k + 1; virtual B() public: void f() std:cout << "in f(): " << i << " " << j << std:endl; public: virtual bool GetTyp
36、e(int& type)/定義B類的標(biāo)示符 type = 1;return true; virtual bool Serialize(int fd) if(-1 = fd)return false;if(:write(fd, &i, sizeof(int) = -1)return false;if(:write(fd, &j, sizeof(int) = -1)return false;return true; virtual ILSerializable* Deserialize(int fd) if(-1 = fd)return NULL;B *p = new B(
37、);int r = :read(fd, &(p->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<ILSerializable*> m_vSerialized;public: bool Serialize(const char *pFilePath
38、, std:vector<ILSerializable*>& v) int fd = :open(pFilePath, O_RDWR | O_CREAT | O_TRUNC, 0);if(-1 = fd)return false;for(int i = 0; i < v.size(); i+)int type;vi->GetType(type);if(:write(fd, &type, 4) = -1)/寫入類別:close(fd);return false;vi->Serialize(fd);if(:close(fd) = -1) return
39、false;return true; bool Deserialize(const char *pFilePath, std:vector<ILSerializable*>& v) int fd = :open(pFilePath, O_RDWR);if(-1 = fd)return false;for(;)int nType;int r = :read(fd, &nType, 4);if(-1 = r) | (0 = r)break;int type;for(int i = 0; i < m_vSerialized.size(); i+)m_vSerializedi->GetType(type);if(type = nType)/判斷類別ILSeria
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年音頻切換臺項目資金籌措計劃書代可行性研究報告
- 革命文化資源融入初中歷史教學(xué)探究-以百色市M中學(xué)為例
- 汽車傳感器與檢測技術(shù)電子教案:液位傳感器
- Brand KPIs for neobanking Freetrade in the United Kingdom-英文培訓(xùn)課件2025.4
- 汽車傳感器與檢測技術(shù)電子教案:檢測的作用與意義
- 幾種類型小鼠缺氧實驗報告范本
- 關(guān)+于儒學(xué)的現(xiàn)代轉(zhuǎn)化問題-評現(xiàn)代新儒家的儒學(xué)轉(zhuǎn)化觀
- 介紹學(xué)院特色活動方案
- 物理中考一輪復(fù)習(xí)教案 十五講 摩擦力 二力平衡
- 從江工會相親活動方案
- 工廠顏色標(biāo)識管理制度
- 2025年環(huán)境科學(xué)與可持續(xù)發(fā)展綜合素質(zhì)評估測試題及答案
- 視頻編輯考試試題及答案
- solidworks考試試題及答案
- 康復(fù)科建設(shè)可行性方案
- 2025年吉林省時事政治考試試卷帶解析及答案(必背)
- 醫(yī)院信息安全法律培訓(xùn)計劃
- 2025國家開放大學(xué)《人類發(fā)展與環(huán)境保護(hù)》形成性考核123答案+終結(jié)性考試答
- 2025至2030年中國環(huán)氧改性硅油行業(yè)投資前景及策略咨詢研究報告
- CR400AFCR400BF型動車組牽引變壓器認(rèn)知及檢查林
- 運(yùn)營支撐服務(wù)合同協(xié)議
評論
0/150
提交評論