DEMO ON FILE HANDLING


12.   DEMO ON FILE HANDLING

 DEMO ON FILE HANDLING
Step 1:-Create a SDI program with name writer to read data from key board.
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.
                                    public:
                                                virtual ~CKeystrokesDoc();
                                                CString Strdata;
Step 3:-Double click on CKeyStrokes.Doc and in that select CWriterDoc() Constructor and initialize the Strdata to an empty string as follows
                                    CWriterDoc::CWriterDoc()
                                                {
                                                            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();
                                    ASSERT_VALID(pDoc);
                                    pDoc->Strdata += nChar;
                                    Invalidate();
                                    CView::OnChar (nChar, nRepeat, nFlags);
                        }
Step 6:-Customize OnDraw() method from CKeystrokes view as follows:
                        void CWriterView::OnDraw(CDC *pDC)
                        {
                                    CWriter* pDoc = Get Document();
                                    ASSERT_VALID(pDoc);
                                    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)
                        {
                                    if(ar.IsStoring())
                                    {
                                                ar<<Strdata;    //TODO: add storing code here
                                    }
                          else
                                    {
                                                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();
                          ASSERT_VALID(pDoc);
                          pDoc -> StrData += nChar;
                          Invalidate();
                          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.
            BOOL CWriter1Doc::OnNewDocument()
            {
                        if (!CDocument::OnNewDocument())
                                                return FALSE;
                        Strdata = “ “;
                        UpdateAllViews(NULL);
                        return TRUE;
            }
Step 10:-Build………..Compile…………Execute.
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.


No comments:

Post a Comment