tags:

views:

60

answers:

2

I was wondering how you open a file in the app bundle from a C++ file.

I.E. I have a file "manifest.xml" that is begin copied into the app bundle. I need a way from C++ to either load this file. I think it is going to involve setting a path some where in Obj-C code so that the file is in the working directory.

A: 

An OS X application is at its heart a Unix application.

How would an application locate its parent directory and open a file on Linux? You'd do it the same way in C++ on OS X.

Now, if you're willing to make a couple of Cocoa or CoreFoundation calls, you can make your life much easier, but if your requirement is that only C++ calls can be used, then then the task is no different than on any other Unix-sytle system.

Wade Williams
+1  A: 

You have to use either CoreFoundation (C) or Foundation (ObjC). Every objects in your app bundle (the "Main bundle") can be accessed using CFBundle/NSBundle functions.

In CoreFoundation you do (NULL-checks omitted):

CFURLRef manifest_url = CFBundleCopyResourceURL(CFBundleGetMainBundle(),
                                                CFSTR("manifest"), CFSTR("xml"),
                                                NULL);
char manifest_path[1024];
CFURLGetFileSystemRepresentation(manifest_url, true,
                                 manifest_path, sizeof(manifest_path));
CFRelease(manifest_url);

FILE* f = fopen(manifest_path, "r"); // etc.

In Foundation you do

NSString* manifest_string = [[NSBundle mainBundle] pathForResource:@"manifest"
                                                            ofType:@"xml"];
const char* manifest_path = [manifest_string fileSystemRepresentation];

FILE* f = fopen(manifest_path, "r");  // etc.
KennyTM