parsingvisual-c++mfctreeviewsdi

Assistance needed in Visual Studio MFC SDI Application


I am a newbie to visual Studio MFC stuffs. Im in a urgent need of creating a small application. I need a help im stuck with this and issue i have a text file which have the following data. A-A1,A2 where A is root node and A1 A2 are the child node. My requirememnt is i need to create a SDI MFC Application. I need like when i go to File->Open->"xxx.txt"(which contains the above data) it must be displayed in the format like A |_ A1 |_ A2 (TREE VIEW). I went through many tutorials like it needs to be tokenized etc. Really im confused ike how to proceed etc.I have done only with dailog based and im new to SDI. Any help is appreciated. Thanks in Advance.


Solution

  • From what I can gather from the comments to the question and the same question linked in Codeproject, I'll try to give a little help, but given how general the question is, the answer will probably not be very specific either.

    First, if you want to have a tree view display, you need your view class to be a CTreeView. A CTreeView is a CView with an embedded CTreeCtrl. A CEditView is a CView with an embedded CEdit, so it's usefull to display text (like a text editor or something like that). A plain CView has no support for any special kind of content, so you have to "draw" it yourself.

    Now, to show something in the view, you have to tell it to display it. Just reading the file won't do it. You have to actively show it. Usually, you'll read your data from the file into some structure, then display it from there. Or you may store your data directly in the tree, it depends. Anyway, you need to learn to use CTreeView/CTreeCtrl. Basically, use CTreeCtrl::InsertItem to add elements.

    As to the tokenization, I'm not sure if I understand your format, but I think I would use different separators for root node and child node. So if you have ROOT-Child1,Child2, I would do something like:

    int pos = 0;
    CString strRoot = strLine.Tokenize(_T("-"), pos);
    
    // do something with strRoot, like store it or display it in the tree
    
    while (pos != -1)
    {
        CString strChild;
    
        strChild = strLine.Tokenize(_T(","), pos);
        // do something with strChild, like store it or display it in the tree
    }
    

    Finally, when working with the Doc/View architecture, the way to go is to separate the data from its display. So you'd typically keep your data in your document, and do all your open/save operations there, and then access the data in the document from the view with GetDocument to display it. Sometimes, it may make sense to have live data in the view, but that's not the usual way of doing it. In such cases it may even make sense to make it a dialog based app instead.