Page 1 of 1

Help - Read lines from text file! [SOLVED]

Posted: Mon Jun 16, 2008 10:13 pm
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

Posted: Mon Jun 16, 2008 10:55 pm
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

Posted: Mon Jun 16, 2008 11:35 pm
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.

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

Posted: Mon Jun 16, 2008 11:36 pm
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

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

Posted: Tue Jun 17, 2008 12:05 am
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

Posted: Tue Jun 17, 2008 12:44 am
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")

Posted: Tue Jun 17, 2008 10:04 am
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!

Posted: Tue Jun 17, 2008 11:15 am
by doublemax
for wxFileInputStream you need to include wfstream.h
for wxTextFile textfile.h

Posted: Tue Jun 17, 2008 12:09 pm
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!

Posted: Tue Jun 17, 2008 1:22 pm
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/