CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2006
    Posts
    34

    save listbox data to a text file.

    Hi all,

    I am trying to save all the contents inside a listbox to a txt file. Unfortunately i can't seem to get it right. The txt file that is read into the listbox contains up to 7,000 lines. I want to be able to edit and save it.

    This is what i've come up with :

    IDC_LIST1 is the listbox control.

    Code:
    	CListBox* p_ListBox = (CListBox*) GetDlgItem(IDC_LIST1);
    	CStdioFile savefile;
    
    	savefile.Open("C://myfile.txt", CFile::modeReadWrite);
    
    	CString theText = "";
    
    
    	p_ListBox->GetWindowText(theText);
    	savefile.WriteString(theText);
    	
    
    	savefile.Close();
    With a little modification it works fine with an editbox. But i can't get it working with a listbox.

    I am using Visual Studio 2003, MFC, Dialog based.

    Any help would be greatly appreciated.

    Thank you

    -- tarja

  2. #2
    Join Date
    Mar 2005
    Location
    Romania,Cluj-Napoca
    Posts
    1,073

    Re: save listbox data to a text file.

    AFAIK you can't enter 7000 items into a list box.... but not 100%.
    Please use code tags [code] [/code]

    We would change the world, but God won't give us the sourcecode..
    Undocumented futures are fun and useful....
    ___
    ______
    Gili

  3. #3
    Join Date
    May 2005
    Posts
    4,954

    Re: save listbox data to a text file.

    Here try this code:
    Code:
      // fill the list
      for ( int ii=0; ii< 50000; ii++ )
      {
        char str[MAX_PATH]={0};
        sprintf(str,"%d",ii);
        m_list.AddString(str);
      }
    
      // write it to file
      FILE *fp = fopen ( "c:\\time.txt","w");
      for (  ii=0; ii< m_list.GetCount(); ii++ )
      {
        char str[MAX_PATH]={0};
        m_list.GetText(ii,str);
        fprintf(fp,"%s\n",str);
      }
      fclose(fp);
    Cheers
    Last edited by golanshahar; August 14th, 2006 at 03:39 PM.
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  4. #4
    Join Date
    Jul 2006
    Posts
    34

    Re: save listbox data to a text file.

    Thank you. That works but one problem. If the file is, say, only 3 lines long, the write code above puts in too many blank spaces. Is there a way for it to know when the end of the file has been reached?

  5. #5
    Join Date
    May 2005
    Posts
    4,954

    Re: save listbox data to a text file.

    Quote Originally Posted by tarja311
    Thank you. That works but one problem. If the file is, say, only 3 lines long, the write code above puts in too many blank spaces. Is there a way for it to know when the end of the file has been reached?
    Your question is not clear, what kind of blanks are you referring to? The code above writes a line and at the end ( \n ) which is end of line

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  6. #6
    Join Date
    Jul 2006
    Posts
    34

    Re: save listbox data to a text file.

    The code above, when reading a 3 lined file, acts as if i hit the enter key 5000 times after the 3rd line.

  7. #7
    Join Date
    May 2005
    Posts
    4,954

    Re: save listbox data to a text file.

    Quote Originally Posted by tarja311
    The code above, when reading a 3 lined file, acts as if i hit the enter key 5000 times after the 3rd line.
    ok now i got it, in the writing loop replace this line:
    Code:
    for (  ii=0; ii< 50000; ii++ )
    with this line:
    Code:
    for (  ii=0; ii<m_list.GetCount(); ii++ )
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  8. #8
    Join Date
    Jul 2006
    Posts
    34

    Re: save listbox data to a text file.

    Awesome! Thank you very much. You've been most helpful. :]

  9. #9
    Join Date
    May 2005
    Posts
    4,954

    Re: save listbox data to a text file.

    You are welcome

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

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

    Re: save listbox data to a text file.

    Well, just a small thing, I sugest using CStdioFile and CString instead of FILE and char*.
    Code:
      CString str;
      // fill the list
      for ( int ii=0; ii< 50000; ii++ )
      {
        str.Format("%d", ii);
        m_list.AddString(str);
      }
    
      // write it to file
      CStdioFile file;
      if(file.Open("text.txt", CFile::modeWrite|CFile::modeCreate))
      {
         int total = (int)m_list.GetCount();
    
         for (  ii=0; ii< total; ii++ )
         {
           m_list.GetText(ii, str);
           file.WriteString(str);
         }
      }
    Marius Bancila
    Home Page
    My CodeGuru articles

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured