wxFileConfig not work as expected

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.
Post Reply
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

wxFileConfig not work as expected

Post by Ronald »

wxFileConfig::wxFileConfig (wxInputStream &is, const wxMBConv &conv=wxConvAuto())
wxFileConfig::Save (wxOutputStream &os, const wxMBConv &conv=wxConvAuto())

One needs wxFileInputStream, another needs wxFileOutputStream
If a wxFileStream is used for both, it fails

OS: Win10
IDE: VS2017

Code: Select all

void MyFrame::OnButtonOK(wxCommandEvent& event)
{
    wxString file_path(wxT("./config.cfg"));
    if (!wxFile::Exists(file_path))
    {
        wxFile f;
        if (!f.Create(file_path))
            return;
    }
    ////////////////////////////////////
    // the code does not work
    wxFileStream fs(file_path);
    bool ok = fs.IsOk();
    wxFileConfig fc(fs);
    int n;
    ok = fc.Read(wxT("/foo/foo"), &n);

    fc.SetPath(wxT("/foo"));
    ok = fc.Write(wxT("foo"), 123);
    fc.SetPath(wxT("/foo/bar"));
    ok = fc.Write(wxT("foo_bar"), wxT("Hi"));
    ok = fc.Write(wxT("foo_bar"), wxT("Hello"));
  
    ok = fc.Save(fs);

    ////////////////////////////////////
    // the code works
    //wxFileInputStream is(file_path);
    //bool ok = is.IsOk();
    //wxFileConfig fc(is);
    //int n;
    //ok = fc.Read(wxT("/foo/foo"), &n);

    //fc.SetPath(wxT("/foo"));
    //ok = fc.Write(wxT("foo"), 123);
    //fc.SetPath(wxT("/foo/bar"));
    //ok = fc.Write(wxT("foo_bar"), wxT("Hi"));
    //ok = fc.Write(wxT("foo_bar"), wxT("Hello"));

    //wxFileOutputStream os(file_path);
    //ok = fc.Save(os);
}
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFileConfig not work as expected

Post by doublemax »

I'm more surprised than the second version works. Why would you save a fileconfig onto itself. You probably missed the Flush() method.

However, if you think this is a bug, open a bug report at http://trac.wxwidgets.org
Use the source, Luke!
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxFileConfig not work as expected

Post by Ronald »

doublemax wrote:Why would you save a fileconfig onto itself.
modify configurations

BTW, I can't register at http://trac.wxwidgets.org, it says:
Warning: reCAPTCHA incorrect. Please try again.
It is blocked by gov.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFileConfig not work as expected

Post by doublemax »

Looking at the wxFileConfig configuration again, i can see where the confusion comes from.

Anyway, this is how i use it:

Code: Select all

wxFileConfig f("", "", wxT("d:\\_test.ini") );

f.SetPath( "/mysql" );
f.Write( "user", "wxwidgets" );
f.Write( "pass", "12345678" );

// does the same
f.Write( "/mysql/user", "wxwidgets" );
f.Write( "/mysql/pass", "12345678" );

// not needed. data will be also written when the object gets destroyed
f.Flush();
Use the source, Luke!
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxFileConfig not work as expected

Post by Ronald »

doublemax wrote:Looking at the wxFileConfig configuration again, i can see where the confusion comes from.

Anyway, this is how i use it:

Code: Select all

wxFileConfig f("", "", wxT("d:\\_test.ini") );

f.SetPath( "/mysql" );
f.Write( "user", "wxwidgets" );
f.Write( "pass", "12345678" );

// does the same
f.Write( "/mysql/user", "wxwidgets" );
f.Write( "/mysql/pass", "12345678" );

// not needed. data will be also written when the object gets destroyed
f.Flush();
a workaround
Post Reply