Thread: list box in mfc, get data from file

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    list box in mfc, get data from file

    I think this is the right place now.

    I am trying to create an application which is a dialog box with a listbox, populated with data from a file.

    Obviously i can create a simple mfc exe for the dialog, and add a listbox, but i dont seem to be able to find the right place to add the comment to put data in it. I was told that an InsertString could be used but I don't know where to put it.

    I am using vc++

    Any help people??
    TIA

    dave

  2. #2
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133

    This almost works right!

    Well here is the example that you asked for:

    First create a drop box.
    Use class wizard to give the drop box a member variable m_drItems.

    Now in your dialog look for CDialog::OnInitDialog(); and place this code there.
    Code:
    	///////////Code starts here/////////////
            char x[1000000];  //make big enough to cover file
            CString buffer;
    	ifstream file;
    
    	//Open file
    	file.open("items.txt", ios::in);  //What ever your file is
    
    	//Check for file exist
    	if(!file)
    	{
    		CString facts = "File not found";
    		MessageBox(facts,NULL,MB_OK);
    	}
    
    	//Read data
    	while(!file.eof())
    	{
    		char x;
    		file.get(x);
    		if(x=='\n')
    		{
    		
    		m_drItems.AddString(buffer);
    		//MessageBox(buffer,NULL,MB_OK); //Test buffer
    		buffer="";
    		}
    		else
    			buffer = buffer + x;
    	}
    	//MessageBox(buffer,NULL,MB_OK); //Test buffer
    	m_drItems.AddString(buffer);
    	m_drItems.SetWindowText(buffer);
    
    	UpdateData(FALSE);
    The problem is with the last entry. It has a strange effect.
    You might have to check for spaces in the file. I didn't include this because of the file I use has no spaces.
    Also you have to #include <fstream.h> at the top under all the other #include's.

    P.S. If anyone know how to fix the last entry so that it is correct, that would be nice. Thanks
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    P.S. If anyone know how to fix the last entry so that it is correct, that would be nice.
    I think it's probably due to the way you are reading the file (get() will read eof but the loop won't be terminated until the remainder of it's body has executed). Change -

    while(!file.eof())

    to -

    while(file.get(x))
    zen

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    That was very fast, thankyou.
    Compilation errors below though, any ideas?

    mitaDlg.cpp
    C:\dave\mita\mitaDlg.cpp(112) : error C2660: 'eof' : function does not take 1 parameters
    C:\dave\mita\mitaDlg.cpp(112) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Error executing cl.exe.

    This was after the changes suggested by zen, without that, it had errors of
    "AddString is not a member of CString"

    C:\dave\mita\mitaDlg.cpp(119) : error C2039: 'AddString' : is not a member of 'CString'
    c:\program files\microsoft visual studio\vc98\mfc\include\afx.h(368) : see declaration of 'CString'

    does this mean that i have missed something out??

  5. #5
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    Change -

    while(!file.eof())

    to -

    while(file.get(x))
    I tried that, no errors, but it turns my input into garbage (jibberish).

    The way that I ended up fixing it was to put a carage return at the end of the .txt file and taking this line of code out:
    Code:
    	//MessageBox(buffer,NULL,MB_OK); //Test buffer
    	m_drItems.AddString(buffer);
    	m_drItems.SetWindowText(buffer);
    Then I moved these lines:
    Code:
            char x[1000000];  //make big enough to cover file
            CString buffer;
    To the top of the file under the line that says:
    Code:
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    //Global variables///////////////
    char x;                      ////  x for buffer to load from file
    CString buffer;              ////  buffer for ifstream
    /////////////////////////////////
    Changed char x[1000000]; to char x;

    >>C:\dave\mita\mitaDlg.cpp(112) : error C2660: 'eof' : function does not take 1 parameters

    This is probly due to the fact of giving this line of code perameters: while(!file.eof()) you have to leave it empty. If you put: while(!file.eof(x)) this could be the problem.

    >>C:\dave\mita\mitaDlg.cpp(119) : error C2039: 'AddString' : is not a member of 'CString'
    >>c:\program files\microsoft visual studio\vc98\mfc\include\afx.h(368) : see declaration of 'CString'

    If you did everythig explaned in the first code + the changes it shoud work. I know it compiles for me.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  6. #6
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133

    I think I found your problem.

    >>C:\dave\mita\mitaDlg.cpp(119) : error C2039: 'AddString' : is not a member of 'CString'
    >>c:\program files\microsoft visual studio\vc98\mfc\include\afx.h(368) : see declaration of 'CString'

    When you created the member variable, did you change the catagory to Control? If you didn't you have to change the catagory to Control from Value. Delete the variable that you created the first time and add another variable and make these changes.

    Sorry about that, I forget what I do behind the code sometimes.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    thanks for all the help,

    just one last thing, my data has gone!!

    the lines are there, but it doesn't show any data - any ideas. i have moved the char x bit to the top, and removed the line you said to. (did you mean the whole section you printed?) either way it left me with no data on show.

    just in case I've missed something here's the code now.

    BOOL CAaDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    ifstream file;

    //Open file
    file.open("items.txt", ios::in); //What ever your file is

    //Check for file exist
    if(!file)
    {
    CString facts = "File not found";
    MessageBox(facts,NULL,MB_OK);
    }

    //Read data
    while(file.get(x))
    {
    char x;
    file.get(x);
    if(x=='\n')
    {

    m_drItems.AddString(buffer);
    //MessageBox(buffer,NULL,MB_OK); //Test buffer
    buffer="";
    }
    else
    buffer = buffer + x;
    }

    UpdateData(FALSE);

    I have "fstream.h", "stdafx.h", "aa.h", "aaDlg.h" included at the start, am I perhaps missing something here?

    I can't see why the data disappears.

    dave

  8. #8
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Try something like this -

    Code:
    ifstream file; 
    char x;
    CString buffer;
    //Open file 
    
    file.open("items.txt", ios::in); //What ever your file is 
     
    
    //Check for file exist 
    if(!file) 
    { 
    	CString facts = "File not found"; 
    	MessageBox(facts,NULL,MB_OK); 
    } 
    
    //Read data 
    while(file.get(x)) 
    { 
    
    	if(x=='\n') 
    	{ 
    		m_drItems.AddString(buffer); 
    		//MessageBox(buffer,NULL,MB_OK); //Test buffer 
    		buffer=""; 
    	} 
    	else 
    	buffer = buffer + x; 
    } 
    
    
    	m_drItems.AddString(buffer); 
    	UpdateData(FALSE);
    assuming m_drItems is your list box control variable.
    zen

  9. #9
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ..although since you're using MFC you could use the CStdioFile class.
    zen

  10. #10
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133

    It compiles ok?

    If it compiles ok, then it could be the location of your .txt file or the contents of the .txt file.

    First the contents of the text file:
    Spaces will cause problems with the control. It will cause empty entries or cause it to be completely empty.
    If the contents have spaces then you have to check for spaces.
    Code:
    //Read data 
    while(file.get(x)) 
    { 
    
    	if(x=='\n') 
    	{ 
    		m_drItems.AddString(buffer); 
    		//MessageBox(buffer,NULL,MB_OK); //Test buffer 
    		buffer=""; //Make sure no spacer in between here. This will clear buffer and make it empty
    	} 
            else if(x==' ') //Make sure you have a space in between the ' '
            {
                    //This will add the item to the list
                    m_drItems.AddString(buffer);
                    buffer="";  //No space
    
                    //To ignore the space, do nothing.  Leave this bracket empty, or comment out the code above.
            }
    	else 
    	buffer = buffer + x;
    Second the location:
    The code that checks file : if(!file) might not check for existance of file. Change it to if(file.fail()). I didn't test it, buy try that if you still have problems email me.
    Last edited by ski6ski; 10-08-2001 at 04:48 PM.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM