Help - Read lines from text file! [SOLVED] 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
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Help - Read lines from text file! [SOLVED]

Post by Double Trouble »

Hi everyone!
I'd appreciate some help on this one! :)
What I'm trying to do is to read specific lines from a text file, say nouns_places.txt. It's Unicode UTF-8 encoding.

nouns_places.txt

いえ
home

学校
がっこう
school

.
.
.


I want my program to read the lines 1 or 5 for example. Later on I'd want my program to read any line 1 + 4n for any n = 0,1,2,...,k. But that is a later issue.

So say I wanna read line 1 or 5 and display it in my window as a static text. I searched the forums and I found out that wxFileInputStream might be the thing, but it doesn't seem to work to well with me! Would you mind helping me out? Maybe showing an example how you'd do it?

Thank you in advance!
/DT
Last edited by Double Trouble on Tue Jun 17, 2008 1:53 pm, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

"doesn't seem to work to well" is not a proper way to describe a problem...

However, if you need random access to the lines in a textfile, you should check this:
http://docs.wxwidgets.org/stable/wx_wxt ... wxtextfile
Use the source, Luke!
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Post by Double Trouble »

doublemax wrote: "doesn't seem to work to well" is not a proper way to describe a problem...
I know that and I'm sorry but the reason is I'm totally new to WxWidgets and with programming I've learned that the best way (for me) to learn is to see examples and analyzing and experimenting with them. So therefore I'd be very glad if someone could show me an example of what I described in my previous post.

I'm in Visual Studio and the intellisense doesn't even recognize wxFileInputStream.
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Re: Help - Read lines from text file! (wxFileInputStream?)

Post by mc2r »

Double Trouble wrote:but it doesn't seem to work to well with me! Would you mind helping me out? Maybe showing an example how you'd do it?
Maybe post an example showing what you ARE doing, and an explanation of what it is doing that you don't want.

-Max
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Re: Help - Read lines from text file! (wxFileInputStream?)

Post by Double Trouble »

If I just add this line to my program

wxFileInputStream input( "text.txt" );

I get the following errors:

Error 1 error C2065: 'wxFileInputStream' : undeclared identifier
Error 2 error C2146: syntax error : missing ';' before identifier 'input'
Error 3 error C3861: 'input': identifier not found
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Try including the appropriate header for wxFileInputStream (it's written in the docs at the wxFileInputStream page)

also, in unicode builds, you'll need to put wxT() around the string literal : wxT("text.txt")
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Post by Double Trouble »

Auria wrote:Try including the appropriate header for wxFileInputStream (it's written in the docs at the wxFileInputStream page)

also, in unicode builds, you'll need to put wxT() around the string literal : wxT("text.txt")
Thank you for the answer!
Of course I am including <wx/txtstrm.h> already! :)
I didn't know about wxT("...") for unicode builds, thank you! But it didn't help me out now. Still got the same errors :?

Thank you in advance!
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

for wxFileInputStream you need to include wfstream.h
for wxTextFile textfile.h
Use the source, Luke!
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Post by Double Trouble »

doublemax wrote:for wxFileInputStream you need to include wfstream.h
for wxTextFile textfile.h
Thanks a lot! It works perfect now!

Here is my code:

Code: Select all

wxFileInputStream input(wxT("text.txt"));
wxTextInputStream text( input );
wxString line;
text >> line;		  
wxStaticText *st = new wxStaticText(panel, wxID_ANY, line);
One problem with this is that it only reads the first word of every line, and not the whole line. Any suggestions?

Thanks a lot for the help! :D

EDIT: I'm looking in to wxTextFile now, but feel free go come with suggestions anyway!
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Post by Double Trouble »

wxTextFile did the thing!

Code: Select all

wxTextFile file;
file.Open(wxT("text.txt"));
wxString line = file.GetLine(1);

wxStaticText *st = new wxStaticText(panel, wxID_ANY, line);
Thanks for all the help! \:D/
Post Reply