Page 1 of 1

How do I save the contents of a wxListBox to a text file?

Posted: Wed Apr 02, 2008 7:03 pm
by papafreebird
I would like to save the contents of a wxListBox to a text file. Using borland c++ builder I am able to accomplish this by using the following code

Code: Select all

ListBox1->Items->GetText();
  ListBox1->Items->SaveToFile(Edit1->Text + "\\gen_avs.bat");
This would save a text file with the contents of the listbox to a text file named gen_avs.bat

For example let's say that I have a wxlistbox that contains the following lines
some text
some more text
and some more text
I would like to be able to save the contents of this listbox to a text file named gen_avs.bat that contains and looks identical to the listbox.
some text
some more text
and some more text
I am now using codeblocks with wxwidgets in ubuntu linux.
How can I accomplish the same thing as I can with borland using wxWidgets wxListBox?

Many thanks for your help.

Posted: Wed Apr 02, 2008 7:20 pm
by lester

Code: Select all

wxArrayString list = ListBox1->GetStrings();
wxTextFile file( _T("gen_avs.bat") );
file.Open();

size_t count = list.Count();
for( size_t i = 0 ; i < count ; ++i )
    file.AddLine( list[ i ] );

file.Write();
file.close();

Posted: Wed Apr 02, 2008 8:36 pm
by papafreebird
Hey thanks for replying. I tried that and got these errors.
error: ‘wxTextFile’ was not declared in this scope|
error: expected `;' before ‘file’|
error: ‘file’ was not declared in this scope|
||=== Build finished: 3 errors, 0 warnings ===|
I don't know if it relates to the version of wxWidgets I'm using or not. I'm using version 2.6 because I had issues with 2.8 compiling right.

Posted: Wed Apr 02, 2008 8:39 pm
by lester
#include <wx/textfile.h>

Posted: Wed Apr 02, 2008 9:18 pm
by papafreebird
Well now don't I feel silly. Should of thought of including that. Much thanks. Compiles without any errors now. I did have to delete the line
file.Open(); line though.
If I leave that line I get an error saying "cannot open file. doesn't exist."
After deleting that line though every thing worked great.

Once again many thanks.

Posted: Wed Apr 02, 2008 9:51 pm
by ArKay
file.Create(const wxString& strFile) should be used for creation instead of Open().

Posted: Wed Apr 02, 2008 10:00 pm
by lester
ArKay wrote:file.Create(const wxString& strFile) should be used for creation instead of Open().
for wxFile, wxFFile but not wxTextFile

Posted: Wed Apr 02, 2008 11:05 pm
by ArKay
Well, the API is kind of weird :D
wxTextFile::Open

bool Open(wxMBConv& conv = wxConvUTF8) const

bool Open(const wxString& strFile, wxMBConv& conv = wxConvUTF8) const

Open() opens the file with the given name or the name which was given in the constructor and also loads file in memory on success. It will fail if the file does not exist, Create should be used in this case.

The conv argument is only meaningful in Unicode build of wxWidgets when it is used to convert the file to wide character representation.
wxTextFile::Create

bool Create() const

bool Create(const wxString& strFile) const

Creates the file with the given name or the name which was given in the constructor. The array of file lines is initially empty.

It will fail if the file already exists, Open should be used in this case.
So you would have to check if the file exists.. If it doesn't, use Create() and if it does, Open(). :lol:

Posted: Thu Apr 03, 2008 12:04 am
by lester
You right - I have miss that

Posted: Thu Apr 03, 2008 12:20 am
by ArKay
So far I've always used wxTextOutputStream for saving so I didn't know that Create() could fail either until I checked. :)

I can't imagine writing code like this :lol:

Code: Select all

wxTextFile file("C:\\blech.txt");
if ((file.Exists() ? file.Open() : file.Create())) 
    ...
    file.Write();
    file.Close();
}