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

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Posts
    42

    display data of text file in list view

    i want to display data from text file in list view and in tree view root will be file name, but i dont know how to do it, the problem is with displaying text file data in list view, i don't know anything about that.
    text file data is very simple
    it is just a square matrix of double values like:

    21.06 34.06 5.0
    12.78 45.25 6.9
    12.89 45.98 5.5

    in list view i want to display it.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: display data of text file in list view

    Have a look at the MFC CListCtrl class
    Victor Nijegorodov

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

    Re: display data of text file in list view

    First, have a look at CStdioFile Class and/or at How to read a text file line by line?.
    Next, to parse each particular line, use CString::Tokenize.
    Finally, fill the listview control, using CListCtrl methods, as Victor already suggested.
    Last edited by ovidiucucu; September 24th, 2012 at 05:25 AM.
    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/

  4. #4
    Join Date
    Jul 2012
    Posts
    42

    Re: display data of text file in list view

    CString str( arr.GetData() ); i tried this and getting errors like

    CString(const class CString &)' : cannot convert parameter 1 from 'class CString *' to 'const class CString &' when i try this
    CAtlString str( arr.GetData() ); it says
    error C2065: 'CAtlString' : undeclared identifier
    E:\MyProjects\my interface\CExerciseDoc.cpp(93) : error C2146: syntax error : missing ';' before identifier 'str'
    E:\MyProjects\my interface\CExerciseDoc.cpp(93) : error C2065: 'str' : undeclared identifier

    when i include header #include "cstringt.h" it says header file not found.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: display data of text file in list view

    Well, why don't you ask for the entire program then? Ovidiu already provided links to examples, so why don't you start by looking at them and give them a try. You shouldn't expect others to do your work. Instead you should try harder by yourself.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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

    Re: display data of text file in list view

    Even advanced programmers can be "totally new" in some circumstances until reading and understanding the documentation. That's programmer's life, often you have to sweat a little instead of asking others to do all the work for you, for free.

    Just giving some more hints:
    • If using MFC, to fill a listview, have a look at CListCtrl::InsertColumn, CListCtrl::InsertItem, and CListCtrl::SetItem / CListCtrl::SetItemText.
    • If not using MFC but plain WinAPI, have a look at LVM_INSERTCOLUMN, LVM_INSERTITEM, and LVM_SETITEM / LVM_SETITEMTEXT.
    • For documetation details and examples, have a search in MSDN Library.
    • Also you can have a look in Codeguru's ListView Control Articles.

    After that, if something isn't yet clear and/or doesn't work as expected, show us what you have tried to do, then we can help you with more details and advices.
    Last edited by ovidiucucu; September 24th, 2012 at 06:32 AM.
    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/

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

    Re: display data of text file in list view

    And here is a quick example, filling a listview control by using MFC:
    Code:
       const int nColCount = 10;
       const int nRowCount = 100;
       CString strText;
    
       // add listview columns
       for(int nCol = 0; nCol < nColCount; nCol++)
       {
          strText.Format(_T("Column %d"), nCol);
          m_listCtrl.InsertColumn(nCol, strText, LVCFMT_LEFT, 100, nCol);
       }
    
       // add and set listview items
       for(int nItem = 0; nItem < nRowCount; nItem++)
       {
          m_listCtrl.InsertItem(nItem, NULL);
          for(int nSubItem = 0; nSubItem < nColCount; nSubItem++)
          {
             strText.Format(_T("Item (%02d, %02d)"), nItem, nSubItem);
             m_listCtrl.SetItemText(nItem, nSubItem, strText);
          }
       }
    Of course, you can notice that's not exactly for copy-paste purpose.
    It's to just to show "HOW TO", like many other examples you can find over the internet, in manuals, documentation, tutorials, and so on.
    Next, you have to sweat a little and adapt it to your specific requirements/needs.
    Last edited by ovidiucucu; September 24th, 2012 at 06:45 AM.
    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/

  8. #8
    Join Date
    Jul 2012
    Posts
    42

    Re: display data of text file in list view

    i dont have any problem with creating right view, thanx for your help but i already know that, what im asking is about reading file and tokenizing and then getting access to that read data for displaying. my questions

    i have tried reading file and have made ReadTextFile(...) member function of listview class
    Q1. is this function called automatically somewhere or i have to call it explicitly
    Q2. if i have to call it explicitly somewhere im confused ,how to provide its parameters i.e LPCTSTR pszFileName, CStringArray& arrLines from where did i get them
    Q3. tokenize is used to parse string so first i have to get string from arrlines right?arrLines.Add(strLine); is used to add string ,which function is better to be used for getting string from arrlines?

    pardon me for asking basics but im quite new to visual c++ thats why.
    Last edited by tspga; September 24th, 2012 at 09:55 AM.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: display data of text file in list view

    Quote Originally Posted by tspga View Post
    i dont have any problem with creating right view, thanx for your help but i already know that, what im asking is about reading file and tokenizing and then getting access to that read data for displaying. my questions

    i have tried reading file and have made ReadTextFile(...) member function of listview class
    Q1. is this function called automatically somewhere or i have to call it explicitly
    Q2. if i have to call it explicitly somewhere im confused ,how to provide its parameters i.e LPCTSTR pszFileName, CStringArray& arrLines from where did i get them
    Q3. tokenize is used to parse string so first i have to get string from arrlines right? which function is better to be used for that purpose?

    pardon me for asking basics but im quite new to visual c++ thats why.
    1) You would have to call it explicitly. Why would you think otherwise?

    2) You know the name of the file, not us. I don't really see why you need a CStringArray at all, but it could be a member of your view class.

    3) ovidiucucu gave you links in post #3.

  10. #10
    Join Date
    Jul 2012
    Posts
    42

    Re: display data of text file in list view

    i have to get the file name from open file dialog so i must call this function in either openfile or open document and then pass it on to this read function
    and question 3 was asked after viewing those links i have viewed but you have not Q3 was about the things not mention in that link

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

    Re: display data of text file in list view

    Is your application SDI, MDI or dialog-based?
    Is your class which keeps the listview control derived from CListView or CListCtrl?
    Last edited by ovidiucucu; September 24th, 2012 at 02:02 PM.
    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/

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

    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
    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/

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

    Re: display data of text file in list view

    [ continued ]

    Step 3: Filling the list.
    When a file has been open, the MFC document-view (SDI/MDI) framework automatically calls the virtual method CView::OnUpdate.
    Bingo! Here is the right place to fill the list.
    What we have to do is to override OnUpdate, get data from the document which is an array of strings, tokenize each string and finally fill thelist. Like a walking in the park.
    Here is the code copleting my example:
    Code:
    class CDemoListView : public CListView
    {
       //...
       // Generated message map functions
    protected:
       DECLARE_MESSAGE_MAP()
       virtual void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint);
    
    // Implementation
    private:
       int _GetListColumnCount() const;
       void _ClearList();
       void _FillList();
    };
    Code:
    void CDemoListView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
    {
       _ASSERT((GetStyle() & LVS_TYPEMASK) == LVS_REPORT); // set report style, dude!
       _ClearList();
       _FillList();
    }
    Code:
    void CDemoListView::_ClearList()
    {
       CListCtrl& listCtrl = GetListCtrl();
       // delete all list items
       listCtrl.DeleteAllItems();
    
       // delete all list columns
       CHeaderCtrl* pHeader = listCtrl.GetHeaderCtrl();
       const int nColCount = _GetListColumnCount();
       if(nColCount > 0)
       {
          for(int nCol = 0; nCol < nColCount; nCol++)
          {
             pHeader->DeleteItem(0);
          }
       }
    }
    Code:
    void CDemoListView::_FillList()
    {
       CListCtrl& listCtrl = GetListCtrl();
       // get the document data
       CDemoDocument* pDoc = GetDocument();
       CStringArray& arrLines = pDoc->GetDocumentData();
       // fill the list
       const int nLineCount = (int)arrLines.GetCount();
       for(int nLine = 0; nLine < nLineCount; nLine++)
       {
          // add an emtpty item
          listCtrl.InsertItem(nLine, NULL);
    
          // now, let's tokenize the line and fill sub-items
          CString strLine = arrLines.GetAt(nLine);
          CString strToken, strCaption;
          int nTokenCount = 0; // count tokens
          int nCurPos = 0;     // used in tokenize
          // first token
          strToken = strLine.Tokenize(_T(" "), nCurPos);
          while(!strToken.IsEmpty())
          {
             ++nTokenCount;
             const int nCol = nTokenCount - 1;
             const int nColCount = _GetListColumnCount();
             if(nTokenCount > nColCount)
             {
                // more tokens than colums, so add a new column
                strCaption.Format(_T("Column %d"), nCol);
                listCtrl.InsertColumn(nCol, strCaption, LVCFMT_LEFT, 100, nCol);
             }
             // set subitem text
             listCtrl.SetItemText(nLine, nCol, strToken);
    
             // next token
             strToken = strLine.Tokenize(_T(" "), nCurPos);
          }
    
       }
    }
    Code:
    int CDemoListView::_GetListColumnCount() const
    {
       CListCtrl& listCtrl = GetListCtrl();
       int nColCount = 0;
       // count the header items in order to get the number of colums
       CHeaderCtrl* pHeader = listCtrl.GetHeaderCtrl();
       if(NULL != pHeader)
       {
          nColCount = pHeader->GetItemCount();
       }
       return nColCount;
    }
    Final notes:
    • This is just a brief example. Although it works, you have to test, complete and solve bugs if any.
    • If something is going wrong or is not enough clear, please do not hesitate to ask!
    • You owe me a couple of beers (virtual, of course).
    Last edited by ovidiucucu; September 25th, 2012 at 05:52 AM.
    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/

  14. #14
    Join Date
    Jul 2012
    Posts
    42

    Re: display data of text file in list view

    thankyou for your help, small and may be last question e.g there is object named sol of type solutions im calling updateAllViews and passing this object, doing type cast that object, after which it automatically goes to onupdate of left view and ryt view, but im stuck with ryt view i dont know how to give that data to onselchnge function that data has to be passed to onupdate ryt? then how should i send it to onselchnge , actually i dont know how to use onupdate of list view with onselchange of left view, kindly try to reply soon its urgent

  15. #15
    Join Date
    Jul 2012
    Posts
    42

    Re: display data of text file in list view

    these are the things i can just read and understand im totally new at this can smone plz give me step by step guide and some sample code, i know how to create list view and adding items and columns but i dont know how to do it with file i have done reading file line by line using other method at some other place for storing values in matrix but i dont know reading here for list view, can smone help me with sample code for the example i quoted in first post

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center