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!
WxTextFile.Write() not performing correctly Topic is solved
Re: WxTextFile.Write() not performing correctly
Code: Select all
myfile.InsertLine(line,myfile.GetCurrentLine());
myfile.RemoveLine(myfile.GetCurrentLine());
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!
Re: WxTextFile.Write() not performing correctly
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
Re: WxTextFile.Write() not performing correctly
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");
myfile[10] = wxT("my new content for line 10");
-
- Earned some good credits
- Posts: 113
- Joined: Sat Sep 24, 2011 9:32 pm
- Location: Poland
Re: WxTextFile.Write() not performing correctly
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
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
Re: WxTextFile.Write() not performing correctly
The problem has been resolved, I opted for:
and that worked. Thanks so much for the help
Code: Select all
myfile[myfile.GetCurrentLine()] = wxT("my new content for line 10");