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

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
papafreebird
Knows some wx things
Knows some wx things
Posts: 27
Joined: Wed Apr 02, 2008 6:54 pm
Location: United States

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

Post 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.
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post 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();
papafreebird
Knows some wx things
Knows some wx things
Posts: 27
Joined: Wed Apr 02, 2008 6:54 pm
Location: United States

Post 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.
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester »

#include <wx/textfile.h>
papafreebird
Knows some wx things
Knows some wx things
Posts: 27
Joined: Wed Apr 02, 2008 6:54 pm
Location: United States

Post 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.
ArKay
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Mar 26, 2008 1:38 pm
Location: Germany

Post by ArKay »

file.Create(const wxString& strFile) should be used for creation instead of Open().
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester »

ArKay wrote:file.Create(const wxString& strFile) should be used for creation instead of Open().
for wxFile, wxFFile but not wxTextFile
ArKay
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Mar 26, 2008 1:38 pm
Location: Germany

Post 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:
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester »

You right - I have miss that
ArKay
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Mar 26, 2008 1:38 pm
Location: Germany

Post 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();
}
Post Reply