File parsing and loading in C++ with wxWidgets Topic is solved

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
Earned a small fee
Earned a small fee
Posts: 24
Joined: Sat Jun 14, 2008 2:22 pm

File parsing and loading in C++ with wxWidgets

Post by joynerCN » Fri Jun 27, 2008 10:32 pm

Alright, I've been trying this for about two hours now and making no headway.

If I have a text file, how do I load it line-by-line into wxStrings? That sounds like a stupid question, but what I've found is that:

a) the standard C++ file input won't load directly into a wxString
b) there's no way (that I can find) to convert directly from string to wxString - wxString only takes char*
c) converting from string to char* to wxString just seems stupidly convoluted
d) no where in the documentation can I find how to actually read from a file using just wxWidgets. I've examined the wxFileInputStream class, but nowhere do I see how to actually read data once it's been declared.
e) I've looked at the wxFile class, which appears to more closely resemble standard stream reading classes, but I still don't see any of the more standard reading methods, like readline.

Is there no way to read a file line-by-line in the more standard way?

wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Fri Jun 27, 2008 11:34 pm


There are a number of ways to do this.
Check this useful page on the wiki which explains how to convert from char* to wxString. ... o_wxString

Post a reply if you're still fighting with it.

Hope that helps,


PS Welcome to the forum.
OS: Vista SP1, wxWidgets 2.8.7.

Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg » Sat Jun 28, 2008 12:12 am

What you want to look into is wxTextFile. It works with text files on a line by line basis. Super simple to use:

Code: Select all

	wxString	file;
	wxFileDialog	fdlog(this);

	if(fdlog.ShowModal() != wxID_OK) return;
	file = fdlog.GetPath();

	wxString	str;
	wxTextFile	tfile;

	str = tfile.GetFirstLine();
		str = tfile.GetNextLine();

wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co

Post by mc2r » Sat Jun 28, 2008 5:15 am

Look at wxTextInputStream, there is an example that shows you how to do this.


Post Reply