WxTextFile.Write() not performing correctly Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
nmartz
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Jul 10, 2012 6:39 pm

WxTextFile.Write() not performing correctly

Post by nmartz »

Hi guys,
I am currently working with a textfile and have been attempting to alter specific lines in the textfile. I have been using code like:

myfile.InsertLine(line,myfile.GetCurrentLine());
myfile.RemoveLine(myfile.GetCurrentLine());

to accomplish what changes I want to make, where line is the wxstring I wish to insert and the current line is the line I wish to remove. I call myfile.Write(); right before I call myfile.Close(); , but my textfile remains exactly as it was prior to running the program. I have tested the code with a few statements like:

if(myfile.Write())
{
wxMessageBox("File has been written");
}

and all those tests return true that the file has been written. Any ideas as to what my problem may be? Thanks for taking the time to help!
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: WxTextFile.Write() not performing correctly

Post by doublemax »

Code: Select all

myfile.InsertLine(line,myfile.GetCurrentLine());
myfile.RemoveLine(myfile.GetCurrentLine());
Are you actually using the GetFirstLine()/GetNextLine() functions? Otherwise GetCurrentLine() will not do what you expect.
http://docs.wxwidgets.org/stable/wx_wxt ... urrentline

And if you just want to change one line, you can do it like this:

Code: Select all

myfile[10] = wxT("my new content for line 10");
Use the source, Luke!
nmartz
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Jul 10, 2012 6:39 pm

Re: WxTextFile.Write() not performing correctly

Post by nmartz »

Yes, I use GetFirstLine() and GetNextLine() to parse through my file but only use GetCurrentLine() to address the place I wish to insert or delete lines
nmartz
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Jul 10, 2012 6:39 pm

Re: WxTextFile.Write() not performing correctly

Post by nmartz »

I would use this code, but my situation kind of prohibits it. I am parsing through an xml file and then taking relevant data and writing it into a text file. Where it is written in the text file is based on what the xml file says, so I do not know in advance which line I am writing to and can't hardcode it.

myfile[10] = wxT("my new content for line 10");
jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: WxTextFile.Write() not performing correctly

Post by jgrzybowski »

I have already checked in wxWidget help, and function InserLine inserts line (your "line") before line number (your "myfile.GetCurrentLine()") and this function does not change current line in file. I guess, probubly you are removing the line which you have inserted one code line above! My suggestions:
1)
myfile.InsertLine(line, myfile.GetCurrentLine());
myfile.RemoveLine(myfile.GetCurrentLine() + 1);
2)
myfile.InsertLine(line, myfile.GetCurrentLine());
myfile.GetNextLine();
myfile.RemoveLine(myfile.GetCurrentLine());

Regards
Jarek
nmartz
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Jul 10, 2012 6:39 pm

Re: WxTextFile.Write() not performing correctly

Post by nmartz »

The problem has been resolved, I opted for:

Code: Select all

myfile[myfile.GetCurrentLine()] = wxT("my new content for line 10");
and that worked. Thanks so much for the help
Post Reply