File open not working on MacOs

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
Rohit Agarwal
Earned some good credits
Earned some good credits
Posts: 119
Joined: Wed May 19, 2021 11:17 pm
Contact:

File open not working on MacOs

Post by Rohit Agarwal »

I'm running MacOS BigSur 11.1
on a MacMini M1 (2020).

I am using a static build (configure --disable-shared)

The following code fails to open a file for writing.
Similar code to open a file for reading also fails.
I tried filename::Exists(). That fails too for a file that visibly exists.
Then I tried this code.
The code runs, nothing gets created.
I'm running this as an Admin, so permissions should not be an issue.

Code: Select all

#include "wx/wxprec.h"
#include "wx/wx.h"
#include "wx/ffile.h"
#include "wx/filename.h"
#include "wx/log.h"

class MyApp : public wxApp
{
public:
  virtual bool OnInit() wxOVERRIDE;
  virtual int OnExit() wxOVERRIDE { return 0; }
};
wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
  if ( !wxApp::OnInit() )
    return false;

  wxFFile *file = new wxFFile();
  unsigned char ucBuffer[22];
  
  file->Open("test", "w");
  file->Write( ucBuffer, 22 );
  file->Close();
  return true;
}
Last edited by doublemax on Thu May 20, 2021 5:11 am, edited 1 time in total.
Reason: Added code tags
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: File open not working on MacOs

Post by doublemax »

I can't test under OSX, but i think the most likely reason is that you're not specifying a path, so the code will probably try to create the while where the executable lies. Maybe that doesn't work?

For testing, try another absolute path, or use wxFileDialog to get one at runtime, and create the file there.
Use the source, Luke!
Rohit Agarwal
Earned some good credits
Earned some good credits
Posts: 119
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: File open not working on MacOs

Post by Rohit Agarwal »

I got it to work.
Using wxFileDialog, I determined the exact path it was using in its return value.
Then I used that exact path to open the file and it worked.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: File open not working on MacOs

Post by PB »

Regardless of the OS, using just a file name is generally a bad idea, as this depends on the current working directory and is rather brittle.

If you do not have a path from the user, you can use wxStandardPaths to obtain a path to a known location:
https://docs.wxwidgets.org/trunk/classw ... paths.html
Post Reply