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

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Things to Remember When Developing 64-bit Software OOO "Program Verification Systems"
C Intro.
Operator Overloading Fundamentals
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
© 2004, D. J. Foreman 1 Program Linking. © 2004, D. J. Foreman 2 Program Content  Given the following file: Float Mysqrt(float); void Prog1 () { extern.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 Review of Chapter 6: The Fundamental Data Types.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
KEAN UNIVERSITY Visual C++ Dr. K. Shahrabi. Developer studio Is a self-contain environment for creating, compiling, linking and testing windows program.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
1 Project 7: Huffman Code. 2 Extend the most recent version of the Huffman Code program to include decode information in the binary output file and use.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Input/Output.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Visual C++ Lecture 11 Friday, 29 Aug Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix,
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Overview of Previous Lesson(s) Over View  Microsoft Foundation Classes (MFC)  A set of predefined classes upon which Windows programming with Visual.
1 C++ Syntax and Semantics, and the Program Development Process.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Bitmap (Chapter 15).
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 3 Input and Output
Chapter 6: FILE I/O and Serialize CFile class. 2 Introduction (1/2) How to read/write a file –Usual way of File I/O Use CFile class Use member functions:
Chapter 8 Dialog Boxes and Property Sheet. 2 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables.
Chapter2: Drawing a Window
Chapter 8 Dialog Boxes and Property Sheet
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class.
Programming with Visual Studio MFC and OpenGL. Outline Creating a project Adding OpenGL initialization code and libraries Creating a mouse event Drawing.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
C is a high level language (HLL)
Microsoft Foundation Classes
Chapter 7 Controls. List box control 3 List Box Control(1/8) Listbox control: –Display lists of text strings called items –Optionally sort the items.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
Hello world !!! ASCII representation of hello.c.
Part II Document/View Architecture. Chapter 9 Documents, Views, and the Single Document Interface.
Operating Systems A Biswas, Dept. of Information Technology.
Computer Interface (Serial) Chung-Buk HRD Institute of KCCI Dept. of Information & Communication PhD. Kang, Won-Chan.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Data in Memory variables have multiple attributes symbolic name
Chapter 1.2 Introduction to C++ Programming
Visual Info Processing Programming Guide (More Details)
Java Beans Sagun Dhakhwa.
EPSII 59:006 Spring 2004.
William Stallings Computer Organization and Architecture 8th Edition
Heterogeneous Data Structures & Alignment
DATA HANDLING.
null, true, and false are also reserved.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 4 Menu and Chapter 6 File I/O
Chapter 5 The MFC Collection Classes
Bits and Bytes Topics Representing information as bits
7. Pointers, Dynamic Memory
Data Structures & Algorithms
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 17 Creating the Document and Improving the View
Lecture 20: Representing Data Elements
Files Chapter 8.
Presentation transcript:

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

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 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 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 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 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 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 Serialization basics (8/8) Concept of serialization CArchive ar(...); ar << a << b; CArchive ar(...); ar >> a >> b; CArchive ObjectCFile Object Local disk a, b

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.

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

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