12. DEMO ON FILE HANDLING
Step 2:-Go to workspace window and select File view tab. Select Header files and click on WriterDoc.h, declare a string of text to hold data in MFC CString object.
Step 3:-Double click on CKeyStrokes.Doc and in that select CWriterDoc() Constructor and initialize the Strdata to an empty string as follows
Strdata = “ “;
Step 4:-To read Key Strokes we have to add an event hanlder
Þ Goto view menu and from that select Class Wizard.
Þ Select CWriterView under class name menu and select WM_CHAR from messages menu and double click it this adds OnChar () event handler.
Step 5:-To store the character the user typed into Strdata, add code to OnChar() method from CWriterView in class view tab double click it.
void CWriterView::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)
CWriterDoc* pDoc = Get Document();
pDoc->Strdata += nChar;
CView::OnChar (nChar, nRepeat, nFlags);
Step 6:-Customize OnDraw() method from CKeystrokes view as follows:
void CWriterView::OnDraw(CDC *pDC)
CWriter* pDoc = Get Document();
pDC-> TextOut(100, 100, pDoc->Strdata);
Step 7:-The file handling deals with the data that has to be send and retrieve from disk for that you are having Serialize() method in WriterDoc.cpp for sending data to disk.
void CWriterDoc::Serialize(CArchive& ar)
ar<<Strdata; //TODO: add storing code here
ar>>Strdata; //TODO: add loading code here
Step 8:-When the user ads some more data to the Strdata object we have to let the document to know that the data has been modified, i.e,
“save changes to document1”
void CWriter1View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
//TODO: Add your message handler code here and/or call default
CWriter1Doc* pDoc = GetDocument();
pDoc -> StrData += nChar;
pDoc -> SetModifiedFlag();
CView::OnChar( nChar, nRepCnt, nFlags);
Step 9:-user can get the new document by adding following code in OnNewDocument() method of WritersDoc.cpp.
Strdata = “ “;
Step 11:-Type the data in the window and select the files menus save as item in dialog box give document name as text1.dat and click on ok and user can use the file menu’s open item to open the saved document and he can made changes to that and can again save it using save item of the file menu.