Page 1 of 1

Problem with wxTextFile!

Posted: Tue Jun 17, 2008 5:38 pm
by Double Trouble
Hi!
I'm gonna describe my problem the best I can. Let's say my program reads a word (a line) from a text file. The word is 4 characters long. If the next word I read (to the same variable) is shorter (say 3 characters) I still get the fourth character left on the end from the last word. And the same goes if the new word is 2 characters but then I get the two last characters from the word with 4 characters. Example:

Word 1 = がっこう (4 characters)
Word 2 = いえ (2 characters)

So if my program first reads がっこう from the text file and then the program reads いえ from the same text file the new word I get isn't いえ but instead I get いえこう


Here is the function for reading lines/words from the text file:

Code: Select all

wxString getstring(int k)
{	
	wxTextFile file;
	file.Open(wxT("text.txt"));
	wxString line = file.GetLine(k);
	return(line);
}
And here's the function that displays a new word when the user presses the button 'Next'. This function calls the function above.

Code: Select all

void Window::OnNext(wxCommandEvent & WXUNUSED(event))
		{
			k = k + 4;
			wxStaticText *st = new wxStaticText(panel, wxID_ANY, getstring(k));
			wxTextFile file;
			file.Open(wxT("text.txt"));
			int nlines = file.GetLineCount();
			
			if(k == nlines - 2){
			k = -2;
			}
						
		}
I've tried to use line = wxEmptyString but the problem remained. So now I'm desperate need of your help!

Thank you in advance!
/DT

Posted: Tue Jun 17, 2008 6:05 pm
by doublemax
a) i'm not quire sure what your program does, but i'd probably read all words into an array once instead of accessing the file for every single word (although it should still work, it'd be just an optimization)

b) are you creating a new wxStaticText instance each time? What happens to the old one? Shouldn't you just change the text of the present wxStaticText? I think you just create new wxStaticText objects on top of each other, so that you still see the previous word under the new one.

Posted: Tue Jun 17, 2008 6:49 pm
by Double Trouble
doublemax wrote:b) are you creating a new wxStaticText instance each time? What happens to the old one? Shouldn't you just change the text of the present wxStaticText? I think you just create new wxStaticText objects on top of each other, so that you still see the previous word under the new one.
Yeah! You must be right there. But I'm really new to this so I don't really know how to write instead. I tried this were I earlier in the code put the line
wxStaticText *st = new wxStaticText(panel, wxID_ANY, getstring(k));
Just to create the new StaticText and then comes this code everytime the user hit the button 'Next' (for the next word)

Code: Select all

void Window::OnNext(wxCommandEvent & WXUNUSED(event))
		{
			k = k + 4;
			st = wxStaticText(panel, wxID_ANY, getstring(k));
			wxTextFile file;
			file.Open(wxT("text.txt"));
			int nlines = file.GetLineCount();
			
			if(k == nlines - 2){
			k = -2;
			}
						
		}
But it doesn't work and I get this error:
error C2440: '=' : cannot convert from 'wxStaticText' to 'wxStaticText *'

Thank you in advance
/DT

Posted: Tue Jun 17, 2008 7:08 pm
by doublemax
1) add a member variable to your Window class, e.g.
wxStaticText *m_statictext;

2) in the ctor of that class, you create one wxStaticText instance:
m_statictext=new wxStaticText(panel, wxID_ANY, wxEmptyString);

3) in your Window::OnNext() method:
m_statictext->SetLabel( getstring(k) );

Posted: Tue Jun 17, 2008 7:58 pm
by Double Trouble
doublemax wrote:1) add a member variable to your Window class, e.g.
wxStaticText *m_statictext;

2) in the ctor of that class, you create one wxStaticText instance:
m_statictext=new wxStaticText(panel, wxID_ANY, wxEmptyString);

3) in your Window::OnNext() method:
m_statictext->SetLabel( getstring(k) );
YES!
Thanks a lot for the help!