PDA

View Full Version : Library/Preferences folder


someone
2007.02.13, 04:25 AM
I'm new to Mac, and i'm trying to port one of my games to that platform. Most of it goes rather smooth, but now I need to write a file to the Library/Preferences dir of the user. I tried google etc, but can't seem to find how to retrieve the correct path to this folder in C++
So i tought i'd post here, anyone here who knows how to do this?
Thanks.

Skorche
2007.02.13, 04:47 AM
getenv("HOME")

I'll assume you can figure it out from there.

OneSadCookie
2007.02.13, 05:44 AM
Naughty, bad, mustn't. There's no guarantee that HOME will be set.

The unix way to find the home folder is to call getpwuid() (read the man page).

The Carbon way to find the Preferences folder is to use the FindFolder function.

Skorche
2007.02.13, 03:05 PM
Really? I've used that all the time. Didn't know that.

unknown
2007.02.13, 03:13 PM
You should really use CFPreferences (well NSUserDefaults but no doubt there will be a "reason" why you cant), http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFPreferences/CFPreferences.html

millercdusa
2007.05.17, 02:31 PM
Here's the code I ended up using for a Carbon application:

// ask the MacOS to find the users preferences folder (/Users/{username}/Library/Preferences/ on US-english MacOS X)
// see http://developer.apple.com/samplecode/MoreFilesX/listing2.html
// and http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html#//apple_ref/c/func/FSFindFolder
// and http://developer.apple.com/samplecode/FileNotification/listing1.html
const int kMaxPath = 1024;
FSRef fsRef;
char path[kMaxPath];
OSErr tError = noErr;

tError = FSFindFolder( kOnAppropriateDisk, kPreferencesFolderType, kDontCreateFolder, &fsRef );
tError = FSRefMakePath( &fsRef, (UInt8 *)path, kMaxPath );