How to Save Listbox Items to a Text File in Vb

Thread: save listbox data to a text file.

  1. August 14th, 2006,02:33 PM #1

    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. August 14th, 2006,02:41 PM #2

    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. August 14th, 2006,02:55 PM #3

    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.

  4. August 14th, 2006,03:25 PM #4

    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. August 14th, 2006,03:27 PM #5

    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


  6. August 14th, 2006,03:31 PM #6

    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. August 14th, 2006,03:39 PM #7

    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

  8. August 14th, 2006,03:42 PM #8

    Re: save listbox data to a text file.

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

  9. August 14th, 2006,03:42 PM #9

    Re: save listbox data to a text file.

    You are welcome

    Cheers


  10. August 15th, 2006,12:49 AM #10

    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);      }   }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules

Click Here to Expand Forum to Full Width



How to Save Listbox Items to a Text File in Vb

Source: https://forums.codeguru.com/showthread.php?397159-save-listbox-data-to-a-text-file

0 Response to "How to Save Listbox Items to a Text File in Vb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel