2007-12-06

Finding Your App's Files

The current working directory for your application depends on how it was run, and what API you're using:

  • If your application is using GLUT, the current working directory is your bundle's Resources directory.
  • If your application is using SDL, the current working directory is the directory containing your application bundle, by default. This is set in SDLMain.m. You may wish to change it (see below).
  • If you run your application from Xcode, the current working directory is the directory containing your application bundle.
  • If you run your application from Finder, the current working directory is /
  • If you run your application from the command-line, the current working directory is the current working directory of the controlling shell.

Generally, you'll want the current working directory to be your bundle's Resources directory. You'll set it correctly once at startup.

Cocoa

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:resourcePath];

CoreFoundation

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
    // error!
}
CFRelease(resourcesURL);
chdir(path);

0 comments: