display data of text file in list view
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 18

Thread: display data of text file in list view

Threaded View

  1. #11
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,059

    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 03:45 AM. Reason: typos
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center