Getting bundle path(MAC)

Hello all,

I want my application to be able to load a file residing in the same directory with the bundle(i.e. So I need to get the current working directory of the bundle… Here is what I do:

char tempPath[512];  
getcwd(tempPath, 512);  

getcwd() from unistd.h returns me the path, but this path is not pointing to the path of the .app bundle but to /…/…/…/ folder(as the main executable is there).

The dirty workaround I employed is to crop the last “” bits with substr() but I was wondering if there is a better, wiser and preferably more cross-platform friendly way around this…


it would be lovely if there were a xplatform way to do this, but the reality is that there isn’t. On *NIX paltforms, you can try using


but that won’t work on Windows and may not give you the correct result depending on what else is going on with your system at the time. The best way to go IMHO is to just find out the most stable platform-specific method and make an xplatform interface. On OSX, you could make a C++ function like

string app_path();  

and in a .mm implementation file use:

// path to application bundle:  
appPath = [[NSBundle mainBundle] executablePath];  
appPath = [appPath stringByResolvingSymlinksInPath];  

or some variation thereof.

its easier than that

just make the path relative to the app rather than absolute.
so in your app’s directory you have



look how ofToDataPath works in ofUtils.cpp - we do the same but add the “data/” to the end because we want to read from the data/ folder.

Hi Theo,
I was looking at ofToDataPath to see what you were talking about. Basically, that just appends a relative path to whatever you pass in, but how does one then use that to write, say, a file to disk? Is there a function I’m not seeing that can handle relative file paths for file I/O or do we have to add that ourselves? Thanks for any help, I’m a bit confused about this right now.

so after doing some research, I see what the deal is. I hadn’t realized that FSPathMakeRef automagically resolves relative paths, which is really nice. I wasn’t able to figure out what environment variable (if any) the relative paths are based on, but it’s not “PWD” so it seems if something freakish were to happen to the PWD env var of the app, FSPathMakeRef wouldn’t break which is an extra bonus. FWIW, using FSPathMakeRef with a relative path is equivalent to using the cocoa code I posted above and concatenating a relative path to it. in this latter case, you’re just resolving the relative path yourself (and thus more work).


Could you explain what you are trying to do.
What type of file are you trying to load?

If it is a file into ofVideoplayer, ofImage or ofSoundPlayer then by default the path should be relative to the data/ directory.

If you are trying to use general file i/o functions like fopen, fread, fwrite then you can just prepend …/…/…/ onto your file path for it to load at the level of the

It would be great if you could say in detail what you are after as I don’t really understand :smiley:


I was basically looking for a generic way to load text files and have been burned by the relative path thing before in GLUT apps, but I’ve figured out why my previous attempts failed and of was working in this regard after doing a little digging and it all came down to FSPathMakeRef. Thanks for the responses!