iDevGames Forums
Carbon File Code - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Carbon File Code (/thread-7734.html)

Carbon File Code - Muffinking - May 28, 2002 01:45 PM

Can someone give me a very simple function that finds a file called "World Map" inside of the folder "Senario Files".

the names don't matter much, but I need some code that creates an FSSpec to the file. The Directory is in the same one as the App.

I just need the bare minimum..
I was told in a previous message I sent that in OS X the file system works differently and when you specify a file by something like "\p:Senario Files:World Map" it doesn't initally look in the apps directory but else where.

I have looked at documentation on the apple site and haven't yet found what I am looking for. I will continue to search until I get a response here.


PS: I am working with the DataFork.

Carbon File Code - Tobi - May 28, 2002 10:17 PM

You first have to find out the directory your application is in. Then create a string with the full pathname of the file and last convert it to an FSSpec.
Here's the code I use for this:

int main(int argc, char **argv)
int a, b;
char programDirectory[1024];
FSRef fsr;
FSSpec fsp;
char fileName[1024];

//First crack out the program directory from the argv parameter
sprintf(programDirectory, "%s", *argv);
for (a=strlen(programDirectory); a> 0; a--)
if (programDirectory[a]=='/'){b++;}
if (b==4){a=0;}
strcat(programDirectory, "/");

//Now create the file name of the file
sprintf(fileName, "%sScenario Files/World Map", programDirectory);

FSPathMakeRef(fileName, &fsr, NULL);//Convert pathname to FSRef
FSGetCatalogInfo (&fsr, NULL, NULL, NULL, &fsp, NULL);//Convert it to FSSpec

//Now you work with your FSSpec (fsp here). Use something like FSpOpenDF() to open it up. Good luck

return 0;

Carbon File Code - GoodDoug - May 30, 2002 01:52 PM

Though, if yopu can swing it, working with FSRefs is much better. FSSpec stuff on MacOS X is pretty hacky, though it can mostly work. There are FSRef calls that are corrollary to most FSSpec file system calls. The only issue is in using Quicktime ro some other API that hasn't moved to FSRefs.

Carbon File Code - wadesworld - May 31, 2002 09:54 PM

Here's what I use. Note that my purpose in this code is to get the top level of the application package directory. Thus, once I get the path to the executable, I chop off the last directory in the path.

CFBundleRef bundle;
    CFURLRef bundle_url;
    CFStringRef    sr;
    char  path[1024];

    bundle = CFBundleGetMainBundle();
    if ( ! bundle )
        goto mac_fail;
    bundle_url=CFBundleCopyBundleURL( bundle );
    if ( ! bundle_url )
        goto mac_fail;
    sr=CFURLCopyFileSystemPath(bundle_url, kCFURLPOSIXPathStyle);
    if ( ! sr )
        goto mac_fail;
    if ( CFStringGetCString(sr, path, 1024, kCFStringEncodingASCII) )
        end=strrchr(path, '/');
        if ( end )
            goto mac_fail;
        if ( chdir(path) )
            goto mac_fail;  //chdir returns -1 if unsucessful

    CFRelease( bundle_url );
    CFRelease( sr );


Carbon File Code - Tobi - Jun 1, 2002 02:27 AM

Both methods should work well. I'm not experienced with all those new CFString..., CFBundle... calls so I'm using a more "traditional" code Smile . The advantage of wadesworld's solution is that you don't need any parameters for your main function and can simply create an "int main(void)".


Carbon File Code - henryj - Jun 3, 2002 12:23 AM

Just this afternoon I decided I should learn how to use the CFBundle stuff for creating file paths. I thought I'd just check idev to see if there was anything useful and bingo.

Thanks wade, that code is exactly what I was after.