[ continued ]
Step 2: Read from text file and fill CDemoDocument::m_arrLines.
One common place to load the document is the overridden CDocument::Serialize.
However, this is not the best place in our case. We can get a CFile*pointer from CArchive argument or the file name. First is no use, because we need CStdioFile* (derived from CFile*). Also, we cannot use the file name because further we canget a share violation.
So better let's override CDocument::OnOpenDocument and simply call _ReadTextFile, similar to the method from that FAQ mentioned earlier.
Code:class CDemoDocument : public CDocument { // ... // Attributes private: CStringArray m_arrLines; // Operations public: CStringArray& GetDocumentData() {return m_arrLines;} // ... // ... // Overrides virtual BOOL OnOpenDocument(LPCTSTR lpszPathName); // Implementation private: void _ReadTextFile(LPCTSTR pszFileName, CStringArray& arrLines); };[ to be continued ]Code:BOOL CDemoDocument::OnOpenDocument(LPCTSTR lpszPathName) { if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE; _ReadTextFile(lpszPathName, m_arrLines); return TRUE; } void CDemoDocument::_ReadTextFile(LPCTSTR pszFileName, CStringArray& arrLines) { arrLines.RemoveAll(); CString strLine; TRY { CStdioFile file(pszFileName, CFile::modeRead); while(file.ReadString(strLine)) arrLines.Add(strLine); } CATCH_ALL(e) { e->ReportError(); // shows what's going wrong } END_CATCH_ALL }


Ovidiu Cucu
Reply With Quote

Bookmarks