wxFileOutputStream will fail if the directory doesn't exist! 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.
Post Reply
tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 165
Joined: Mon Apr 24, 2017 4:23 am

wxFileOutputStream will fail if the directory doesn't exist!

Post by tomay3000 » Mon May 04, 2020 10:42 am

Hello,
When creating a file using wxFileOutputStream and the directory in which this file will be created doesn't exist in the first place, then wxFileOutputStream::IsOk will return false and the file won't be created of course.

Is this the intended behavior? because nothing is said in the documentation.

TIA.

User avatar
doublemax
Moderator
Moderator
Posts: 14786
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFileOutputStream will fail if the directory doesn't exist!

Post by doublemax » Mon May 04, 2020 12:35 pm

Yes, and that's true for all file creating classes/functions (e.g. in wxFile, wxFFile etc.). It's your responsibility to create the folder structure. wxFileName::Mkdir() with wxPATH_MKDIR_FULL flag can help with that:
https://docs.wxwidgets.org/trunk/classw ... df53d6f3be
Use the source, Luke!

tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 165
Joined: Mon Apr 24, 2017 4:23 am

Re: wxFileOutputStream will fail if the directory doesn't exist!

Post by tomay3000 » Tue May 05, 2020 1:02 am

doublemax wrote:
Mon May 04, 2020 12:35 pm
wxFileName::Mkdir() with wxPATH_MKDIR_FULL flag can help with that
Thank you,
wxFileName::Mkdir with wxPATH_MKDIR_FULL flag is really handy, the question is: should I test for that directory existence before!? using wxDirExists or it is done internally in wxFileName::Mkdir implementation!
I need to dig into the source code.

TIA.

User avatar
doublemax
Moderator
Moderator
Posts: 14786
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFileOutputStream will fail if the directory doesn't exist!

Post by doublemax » Tue May 05, 2020 5:51 am

If the flags contain wxPATH_MKDIR_FULL flag, try to create each directory in the path and also don't return an error if the target directory already exists.
Use the source, Luke!

tomay3000
I live to help wx-kind
I live to help wx-kind
Posts: 165
Joined: Mon Apr 24, 2017 4:23 am

Re: wxFileOutputStream will fail if the directory doesn't exist!

Post by tomay3000 » Tue May 05, 2020 7:38 pm

doublemax wrote:
Tue May 05, 2020 5:51 am
If the flags contain wxPATH_MKDIR_FULL flag, try to create each directory in the path and also don't return an error if the target directory already exists.
My bad, I haven't read that description to the end.

Post Reply