I want my application to be able to load a file residing in the same directory with the bundle(i.e. openframeworks.app). So I need to get the current working directory of the bundle… Here is what I do:
getcwd() from unistd.h returns me the path, but this path is not pointing to the path of the .app bundle but to /…/…/…/openframeworks.app/Contents/Resources/ folder(as the main executable is there).
The dirty workaround I employed is to crop the last “openframeworks.app/Contents/Resources/” 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
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).
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!