none
How do you save data from a listbox to a text file?

    Question

  •  Hi,

    I want to save the data thats in a listbox to a text file. This is what I have so far:

    void CTtoSDlg::OnBnClickedButton5()
    {
        CWnd* pWnd = GetDlgItem(IDC_EDIT2);
        UpdateData();
        CFile f;
        CFileDialog FileDlg(FALSE, _T(
    ".txt"), NULL, 0, (_T("Text file (*.txt)|*.txt|All 
            files (*.*|*.*||"
    )));

        if (FileDlg.DoModal() == IDOK)
        {
            f.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite);
            f.Write(); // Here is the problem
            pWnd->SetWindowText(FileDlg.GetOFN().lpstrFileTitle);
        }

        else 
            return;

        f.Close();
    }

    How will I use f.Write ?

    Monday, July 14, 2008 11:52 AM

Answers

  • You'll need to loop through the listbox items one-by-one.  That's just the way listbox works; there's no easy way to get all the text out at once.

    I would also use CStdioFile because it provides the more convenient WriteString method, in addition to the raw Write.

       CFileDialog fileDlg(FALSE, _T(".txt"), NULL, 0, _T("Text File (.txt)|*.txt|"
          _T("All files (*.*)|*.*||")); 
     
       if (fileDlg.DoModal() == IDOK) 
       { 
          const int numItems = m_ListBox.GetCount(); 
          CString itemText; 
          CStdioFile file; 
     
          if (file.Open(fileDlg.GetFileName(), CStdioFile::modeCreate | CStdioFile::modeWrite)) 
          { 
             for (int i = 0; i < numItems; ++i) 
             { 
                m_ListBox.GetText(i, itemText); 
                file.WriteString(itemText); 
                file.WriteString(_T("\n")); 
             } 
     
             file.Close(); 
          } 
       } 
     

    • Marked as answer by Johannekie Tuesday, July 15, 2008 7:37 AM
    Monday, July 14, 2008 1:16 PM

All replies

  • Unfortunately, the link to the online docs for CFile::Write seems to be broken.

    Here's an example.

    I don't remember whether the second argument is the length of the buffer in characters or the length in bytes.

    Edit: this page seems to indicate it's the length in bytes.

    • Edited by Nick Meyer Monday, July 14, 2008 12:32 PM added last line
    Monday, July 14, 2008 12:30 PM
  • Thanks for the example. I looked at it, but I still don't know how to write all the entries in the listbox to the file.

    I thought of using strcpy to copy the entries and then use f.write to write them to the file. But then I have to do the entries one by one, isn't there a way that I can copy everything in the listbox to a file all at once. 
    Monday, July 14, 2008 12:47 PM
  • You'll need to loop through the listbox items one-by-one.  That's just the way listbox works; there's no easy way to get all the text out at once.

    I would also use CStdioFile because it provides the more convenient WriteString method, in addition to the raw Write.

       CFileDialog fileDlg(FALSE, _T(".txt"), NULL, 0, _T("Text File (.txt)|*.txt|"
          _T("All files (*.*)|*.*||")); 
     
       if (fileDlg.DoModal() == IDOK) 
       { 
          const int numItems = m_ListBox.GetCount(); 
          CString itemText; 
          CStdioFile file; 
     
          if (file.Open(fileDlg.GetFileName(), CStdioFile::modeCreate | CStdioFile::modeWrite)) 
          { 
             for (int i = 0; i < numItems; ++i) 
             { 
                m_ListBox.GetText(i, itemText); 
                file.WriteString(itemText); 
                file.WriteString(_T("\n")); 
             } 
     
             file.Close(); 
          } 
       } 
     

    • Marked as answer by Johannekie Tuesday, July 15, 2008 7:37 AM
    Monday, July 14, 2008 1:16 PM
  • Thank you so much for the code. I really appreciate it. I posted a new thread on multi column listboxes. If you could just please take a look at that for me.
    Tuesday, July 15, 2008 7:34 AM