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

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

Embed Size (px)

Citation preview

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

Chapter 6:FILE I/O and Serialize

CFile class

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

FILE I/O

Serialization and the CArchive Class

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

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).”

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

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));

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

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;

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

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);

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

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);

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

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) ;

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

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

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

10

Serialization basics (8/8)

• Concept of serialization

CArchive ar(...);ar << a << b;

CArchive ar(...);ar >> a >> b;

CArchive Object CFile ObjectLocal disk

a, b

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

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.

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

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”.

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

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();};

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

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

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

15

Serialization Implementation (3/5)

• Change your class to support the serialization// in your header fileclass 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); ④};

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

16

Serialization Implementation (4/5)

• Change your class to support the serialization (cont'd)// In your cpp fileCMyData::~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;}

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

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

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

Downside of the Serialize

• The whole file should be read• The whole file should be written• Extra information should be stored.