Originally just for the Mac platform to simply call fopen, fwrite and other functions, found that fopen need to include an absolute path to normal use, otherwise, even if you can create a file during debugging, directly call the app execution, the file will not be created.
To do this, you need to get an absolute path to the execution file. Use the function _nsgetexecutablepath. The path that this function obtains is the path of the executable file in the app package, which is added directly to the following. /.. /.. /.. /' to the app path is also not possible, probably the problem of access rights, will cause fopen failure.
So first the obtained path processing, get the app path, and then call in fopen, confirmed OK.
The code is as follows:
Get app path. #include <mach-o/dyld.h>char g_path[maxpathlen+1];-(void) getapppath{ uint32_t size = sizeof ( G_path); if (_nsgetexecutablepath (G_path, &size) = = 0) printf ("Executable path is%s\n", g_path); else printf ("Buffer too small; Need size%u\n ", size); /users/blade/proj//output/macos/bin/testtool.app/contents/macos/testtool if (strlen (G_path)) { int end,count = 0; for (int i = strlen (G_path)-1; i>0; i--) { if (g_path[i] = = '/') { count++; } if (count = = 4) { end = i; break; } } if (End > 0) { memset (G_path+end,0,strlen (G_path)-end); } }}
Mac platform Get app Path