The problem is that a .app
is a package, and therefore it is actually a directory structure as well, not a "normal" file. Your actual executable is inside the directory YourApp.app/Contents/MacOSX/
. Thus when you use a relative path starting with ./
to open a file in C-code, the executable is actually looking for your file inside the YourApp.app/Contents/MacOSX/
directory, and not the parent directory that the .app
package is installed in.
You can actually browse the directory structure of your .app
by right-clicking on it and choosing View Package Contents
.
If you are going to place files in the file-system that you would like accessible from your executable, either package them inside the .app
, or place them outside the .app
, and place enough ../
in your file access to get you out of the .app
directory structure and into the parent directory where the .app
is installed.
If you want your /data
directory to be inside the .app
package, then you would only have to add enough ../
to your path to get you to out of your /MacOSX
directory and into the root of the .app
where the /data
directory would be found.
Finally, if you need to know the absolute path where your executable is located, you can always use the C-function _NSGetExecutablePath
found inside of mach-o/dyld.h
(i.e., you don't need objective-C). From there you can modify the path to get at any other directory in the file-system relative to where your executable is by trimming it to the proper parent directory and then appending the path name to the file you want to open.