|
-
September 25th, 2012, 02:38 AM
#11
Re: display data of text file in list view
[ 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);
};
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
}
[ to be continued ]
Last edited by ovidiucucu; September 25th, 2012 at 02:45 AM.
Reason: typos
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|