Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class.

Similar presentations


Presentation on theme: "Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class."— Presentation transcript:

1 Chapter 6: FILE I/O and Serialize CFile class

2 FILE I/O Serialization and the CArchive Class

3 3 Serialization basics (1/8) –serialization is … (from Wikipedia) the process of saving an object onto a storage medium (such as a file, or a memory buffer) to transmit it across a network connection link in binary form. The series of bytes or the format can be used to re-create an object that is identical in its internal state to the original object (actually, a clone).”

4 4 Serialization basics (2/8) Read data by using CFile CFile file; CFileException e; if(!file.Open("mytest.dat", CFile::modeReadWrite|CFile::modeCreate, &e)) { e.ReportError(); return } int a = 100; int b = 200; file.Write(&a, sizeof(a)); file.Write(&b, sizeof(b));

5 5 Serialization basics (3/8) Read data by serialization CFile file; CFileException e; if(!file.Open("mytest.dat", CFile::modeReadWrite|CFile::modeCreate, &e)) { e.ReportError(); return; } int a = 100; int b = 200; CArchive ar (&file, CArchive::store); ar << a << b;

6 6 Serialization basics (4/8) Write data by using CFile CFile file; CFileException e; if(!file.Open("mytest.dat", CFile::modeRead, &e)) { e.ReportError(); return; } int a, b; file.Read(&a, sizeof(a)); file.Read(&b, sizeof(b)); TRACE("a = %d, b = %d\n", a, b);

7 7 Serialization basics (5/8) Write data by serialization CFile file; CFileException e; if(!file.Open("mytest.dat", CFile::modeRead, &e)) { e.ReportError(); return; } int a, b; CArchive ar (&file, CArchive::load); ar >> a >> b; TRACE("a = %d, b = %d\n", a, b);

8 8 Serialization (6/8) CArchive Class constructor: –pFile Pointer to CFile object –nMode CArchive::load or CArchive::store –nBufSize Buffer size (don’t need to change it) –lpBuf Buffer address (don’t need to change it) CArchive::CArchive (CFile* pFile, UINT nMode, int nBufSize = 4096, void* lpBuf = NULL) ;

9 9 Serialization basics (7/8) Data types ready to the serialization Data types Basic data types BYTE, WORD, LONG, DWORD, float, double, int, short, char, wchar_t, unsigned, bool, ULONGLONG, LONGLONG MFC data types RECT, POINT, SIZE, CRect, CPoint, CSize, CString, CTime, CTimeSpan, COleVariant, COleCurrency, COleDateTime, COleDataTimeSpan

10 10 Serialization basics (8/8) Concept of serialization CArchive ar(...); ar << a << b; CArchive ar(...); ar >> a >> b; CArchive ObjectCFile Object Local disk a, b

11 Coding practice I Make a program for typing Mouse left click: Save the contents as “testString.dat” file Mouse right click: Load the contents from “testString.dat” file and show it.

12 Coding practice II Draw many circles by using mouse left clicks. Add menu ‘Save’ and save the information of the circles as “circle.dat” file Add menu ‘Load’ and load the data from the file “circle.dat”.

13 13 Serialization Implementation (1/5) How to make your own class to support the serialization? class CMyData { public: CString m_str; COLORREF m_color; public: CMyData(CString &str, COLORREF &color) { m_str = str; m_color = color; } virtual ~CMyData(); };

14 14 Serialization Implementation (2/5) It will not support any serialization by default void CYourProgram::SaveOrLoad(CArchive& ar) { if (ar.IsStoring()) { ar << m_data; } else { ar >> m_data; } X

15 15 Serialization Implementation (3/5) Change your class to support the serialization // in your header file class CMyData : public CObject ① { DECLARE_SERIAL(CMyData) ② public: CString m_str; COLORREF m_color; public: CMyData() { } ③ CMyData(CString &str, COLORREF &color) { m_str = str; m_color = color; } virtual ~CMyData(); void Serialize(CArchive& ar); ④ };

16 16 Serialization Implementation (4/5) Change your class to support the serialization (cont'd) // In your cpp file CMyData::~CMyData() { } IMPLEMENT_SERIAL(CMyData, CObject, 1) ⑤ void CMyData::Serialize (CArchive& ar) ⑥ { CObject::Serialize(ar); if(ar.IsStoring()) ar << m_str << m_color; else ar >> m_str >> m_color; }

17 17 Serialization Implementation (5/5) Now your own class support serialization void CYourProgram ::SaveOrLoad(CArchive& ar) { if (ar.IsStoring()) { m_data.Serialize(ar); } else { m_data.Serialize(ar); } O

18 Downside of the Serialize The whole file should be read The whole file should be written Extra information should be stored.


Download ppt "Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class."

Similar presentations


Ads by Google