file.write is erasing all text 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.
papayrus
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 204
Joined: Tue Jan 25, 2011 4:55 pm
Location: USA
Contact:

file.write is erasing all text

Post by papayrus » Wed Jan 22, 2014 7:15 pm

I made this program that changes the player name in an ini file and it was working before but for some reason now whenever I try to write the playername it erases all the text in the file. Here is my code. I also added the rev.ini file. It's not that important it's just a fun project but I would like to know why it is just erasing all the text in the rev.ini file now. I am using wxWidgets-2.9.4.

Code: Select all

void L4D2_Name_ChangerFrame::OnTextCtrl1TextEnter(wxCommandEvent& event)
{
    if( !wxFileName::FileExists( wxT("..\\rev.ini") ) )

    {
        wxMessageBox( _("Cannot locate rev.ini"));
    }
    else
    {
        //if (TextCtrl1->IsEmpty())
    if (TextCtrl1->GetValue() == (_("Player Name")))
    {
        //TextCtrl1->SetValue(wxT("Player Name"));
        wxMessageBox(wxT("OOPS! No name entered"));
        return;
    }
    if (TextCtrl1->IsEmpty())
        {
        //TextCtrl1->SetValue(wxT("Player Name"));
        wxMessageBox(wxT("OOPS! No name entered"));
        return;
    }

    wxTextFile tfile;
    //tfile.Open(_("REV\\rev.ini"));
    tfile.Open(_("..\\rev.ini"));

    for (size_t n=0; n < tfile.GetLineCount(); ++n)
    {
        wxString &line = tfile.GetLine(n);
        //wxLogMessage( wxT("Line %d: %s"), n, line.c_str());
        if( line.StartsWith( wxT("PlayerName =") ) )
            {
                //wxLogMessage( wxT("interesting line found"));
                line = ( wxT("PlayerName =") ) + TextCtrl1->GetValue();
            }
    }
    tfile.Write();
    wxMessageBox (wxT("Good Job!!!\nNow Play L4D2"));
    }
}

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: file.write is erasing all text

Post by PB » Wed Jan 22, 2014 7:44 pm

I couldn't find any code that could cause the problem you described. Out of curiosity, what number tfile.GetLineCount() returns after tfile.Open() and before tfile.Write() calls?

papayrus
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 204
Joined: Tue Jan 25, 2011 4:55 pm
Location: USA
Contact:

Re: file.write is erasing all text

Post by papayrus » Wed Jan 22, 2014 11:14 pm

PB wrote:I couldn't find any code that could cause the problem you described. Out of curiosity, what number tfile.GetLineCount() returns after tfile.Open() and before tfile.Write() calls?
I do not know and also the log messages don't seem to work.

papayrus
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 204
Joined: Tue Jan 25, 2011 4:55 pm
Location: USA
Contact:

Re: file.write is erasing all text

Post by papayrus » Wed Jan 22, 2014 11:30 pm

OK They do work now it is line 81.
I uncommented

Code: Select all

wxLogMessage( wxT("Line %d: %s"), n, line.c_str());
and it says line 81. I opened it in notepad++ and it shows the last line of text as 81. Line 41 is where PlayerName = is. I don't get it either why does it just erase all the text in the ini?
If I uncomment

Code: Select all

wxLogMessage( wxT("interesting line found"));
It just seems to ignore it and skips right to the write file and still erases all the text in the ini file. I removed one line of text in the ini file and that is when it started erasing all the text in the ini file. It works fine with the old ini that has one extra line of text in it. The line of text is just the one that says Language = English because I accidently had it written twice in the ini.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: file.write is erasing all text

Post by PB » Thu Jan 23, 2014 2:51 pm

The code looks fine to me and hearing there are some other unexpected issues, there may be something wrong on higher scope than just the function you posted, causing some random/wrong behaviour. Assuming you used Connect() to specify the event handler, is the event sink correct? But if it were not, the code would explode on an attempt to access TextCtrl... :S

papayrus
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 204
Joined: Tue Jan 25, 2011 4:55 pm
Location: USA
Contact:

Re: file.write is erasing all text

Post by papayrus » Thu Jan 23, 2014 5:11 pm

Here is the difference between the 2 rev.ini files this first one works fine still.

Code: Select all

[Emulator]

# Language - rev emu will automagically load the correct language files. 
# This can be English, French, Italian, German, Spanish, sChinese, 
# Korean, Koreana, tChinese, Japanese, Russian, Thai, or Portugeuse
#Language = English
Language = English
SteamDll=bin\Steam_orig.dll                
SteamClient = True
SteamUser = Clicks5

[Log]

Logging=False  
FileSystem=False
Account=False
UserID=False
 
[steamclient]

#Change the setting below to set your PlayerName
PlayerName =Daddy

#Add rename ClanTag maximum 12 symbol UTF-8 or 24 symbol ANSI
#ex. ClanTag = revcrev 
ClanTag=[revcrew]

SteamExe=bin\Steam.exe 

#Change the setting to set masterserver
#HL2 servers
#Default Rev MasterServer For Setti Switch To 46.4.71.67:27011
MasterServer =208.64.200.52:27011    
#MasterServer =46.4.71.67:27011
#MasterServer =63.234.149.90:27011
#
#Search game version in MasterServer.
#Default search all version.
#
# Use avatar enable. Test images no-steam avatar in platform/avatar1.dat(delete\rename it`s file not show this images) 
# Default = False
Use_avatar = true
#
#GameVersion = 2.1.0.4 - Ignore This
But this second one here is the one that gets erased doing the same thing. This is the one I need to use now though. I am just changing the PlayerName = name after that. I see there is one extra line in the second rev.ini that is language english and I see there is more spaces as well could this have something to do with it?

Code: Select all

[Emulator]



# Language - rev emu will automagically load the correct language files. 

# This can be English, French, Italian, German, Spanish, sChinese, 

# Korean, Koreana, tChinese, Japanese, Russian, Thai, or Portugeuse

Language = English 

SteamDll=bin\Steam_orig.dll                

SteamClient = True

SteamUser = SFKTeam



[Log]



Logging=False  

FileSystem=False

Account=False

UserID=False

 

[steamclient]



#Change the setting below to set your PlayerName

PlayerName =Daddy



#Add rename ClanTag maximum 12 symbol UTF-8 or 24 symbol ANSI

#ex. ClanTag = revcrev 

ClanTag=[revcrew]



SteamExe=bin\Steam.exe 



#Change the setting to set masterserver

#HL2 servers

#Default Rev MasterServer For Setti Switch To 46.4.71.67:27011

MasterServer =208.64.200.52:27011    

#MasterServer =46.4.71.67:27011

#MasterServer =63.234.149.90:27011

#

#Search game version in MasterServer.

#Default search all version.

#

# Use avatar enable. Test images no-steam avatar in platform/avatar1.dat(delete\rename it`s file not show this images) 

# Default = False

Use_avatar = true


PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: file.write is erasing all text

Post by PB » Thu Jan 23, 2014 5:44 pm

I don't know what version of wxWidgets are you using, but this bugfix may be of interest:
http://trac.wxwidgets.org/changeset/75388

From the file contents you pasted it seems the line spacing is different between them, so perhaps the two files are using different end of line separators?

papayrus
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 204
Joined: Tue Jan 25, 2011 4:55 pm
Location: USA
Contact:

Re: file.write is erasing all text

Post by papayrus » Thu Jan 23, 2014 6:09 pm

Using version wxWidgets-2.9.4. It definitly has something to do with the spacing because if I put all the lines together it works fine without any spacing between lines. Problem is I have to work with it the way it is.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: file.write is erasing all text

Post by PB » Thu Jan 23, 2014 6:15 pm

Do you really have to use those old wxWidgets? I definitely recommend upgrading to the recent version with the bugfix mentioned above and trying again. If you have a serious reason why you can't upgrade, either apply the patch to your version (as you can see it is very simple) or use a different way how to process the file.

papayrus
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 204
Joined: Tue Jan 25, 2011 4:55 pm
Location: USA
Contact:

Re: file.write is erasing all text

Post by papayrus » Thu Jan 23, 2014 6:25 pm

I have no idea what I need to patch my 2.9 wxWidgets or how to patch it. The link you gave looks like it is for 3.0. I will be getting 3.0 up need to find a good tutorial on how to do it with the new code blocks.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: file.write is erasing all text

Post by PB » Thu Jan 23, 2014 7:04 pm

I am not sure if the issue I linked before is the one causing your problem. Attach (not paste the contents of) the problematic file and I will check if I can write it properly.

papayrus
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 204
Joined: Tue Jan 25, 2011 4:55 pm
Location: USA
Contact:

Re: file.write is erasing all text

Post by papayrus » Thu Jan 23, 2014 7:16 pm

It says ini is not allowed for attaching so I changed it to .txt. You will have to rename the extension back to .ini.
Attachments
rev.txt
(1.12 KiB) Downloaded 91 times

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: file.write is erasing all text

Post by PB » Thu Jan 23, 2014 7:32 pm

Code: Select all

 wxString fileName("c:\\rev.ini");

        wxTextFile tf;

        if ( tf.Open(fileName) )
        {
            tf.Write();
        }
The file data are preserved, but the saved file is 3 bytes shorter. The difference is the original file has a UTF-8 BOM header, the resulting one does not.

Edit
wxTextFile does not support BOM header, see https://groups.google.com/d/msg/wx-user ... tcHK4YlnQJ .

As you have to preserve the file content (other programs may expect the BOM), you probably can not use wxTextFile for what are you doing. I do not know how other wxWidgets file classes (e.g. wxTextOutputStream) handle that.

papayrus
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 204
Joined: Tue Jan 25, 2011 4:55 pm
Location: USA
Contact:

Re: file.write is erasing all text

Post by papayrus » Fri Jan 24, 2014 4:50 am

How did you figure out that it is UTF 8 BOM and how can I convert the file? Is it possible to convert the file header and is it possible to convert it vis code in wxwidgets?

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: file.write is erasing all text

Post by PB » Fri Jan 24, 2014 6:32 am

papayrus wrote:How did you figure out that it is UTF 8 BOM and how can I convert the file? Is it possible to convert the file header and is it possible to convert it vis code in wxwidgets?
I looked at the original file in the hexadecimal viewer and noticed the BOM there. For example, if you open such file in Windows Notepad, it doesn't display the mark, but if you attempt to save the file under a different name it will default to UTF-8 encoding in the file save dialog.

You should be able to do what you want. E.g. load the strings, modify them and then write the BOM manually using one of the other wxWidgets file/stream classes. You just need to read wxWidgets docs and decide which way (and class) is the best for you. Just be sure to load and save the file using UTF-8 encoding.

Post Reply