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.
-
What do you need help with? Opening the file? Parsing it? Putting data in a tree view? Dealing with MFC Doc/View architecture? All of the above? – MikMik Mar 26 '13 at 09:34
-
Yes i need help with parsing my text file and putting in a tree. Im stuck der for two days :( Can u please teach me how to do it. – maxer zaurak Mar 26 '13 at 11:52
-
You should post some code, so we know what you have tried already and help you on that. Or maybe you should ask separate questions for the separate parts of the problem. Parsing a file is one thing, displaying data on a tree view is another, integrating all that in a Doc/View application is another... – MikMik Mar 26 '13 at 12:45
-
here is the code which i have tried out .. and one more thing make me clear na .. while creating mfc SDI application the base class i hav given as CEditView is that okay for that purpose or should i change anyother??? – maxer zaurak Mar 27 '13 at 04:07
-
void CFileView::OnFileOpen() { // TODO: Add your command handler code here CString pathname,strLine; CStdioFile File; if(File.Open(pathname, CFile::modeRead)) // Open file to be read { while(File.ReadString(strLine)) // Read file { int Position = 0; CString Token; Token = strLine.Tokenize(_T("-"), Position); while(Token!=" ") { _tprintf_s(_T("Resulting token: %s\n"), Token); Token = strLine.Tokenize(_T("-"), Position); } } – maxer zaurak Mar 27 '13 at 04:10
-
http://www.codeproject.com/Questions/568215/TextplusFileplusOpenplusinplusSDI Please see the link for more details – maxer zaurak Mar 27 '13 at 07:18
2 Answers
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.

- 3,426
- 2
- 23
- 41
Basically i have worked in the CTreeCtrl in the dailog based applications but this is the first time me working in SDI. I would really thank you for providing me such a solution. I have developed a small piece of code have a look at it
void CFileView::OnFileOpen()
{
// TODO: Add your command handler code here
CString pathname,strLine;
CStdioFile File;
if(File.Open(pathname, CFile::modeRead)) // Open to read
{
while(File.ReadString(strLine)) // Read file
{
int Position = 0;
CString Token;
Token = strLine.Tokenize(_T("-:,"), Position);
HTREEITEM hRoot = m_wndFileView.InsertItem(Token, 0, 0);
while(Token!="")
{
Token = strLine.Tokenize(_T("-:,"), Position);
HTREEITEM hSrc = m_wndFileView.InsertItem(Token, 0, 0, hRoot);
}
}
}
}
Like my point is am i placing my code in the correct function? i.e where we open the file. Beacuse in the client window i am not getting anything rather a blank window. SDI is based CTreeView class. I am now clear with the tokenizer and the tree control part but now i am confused whynot i not at all getting anything in the client window :(

- 3
- 2