View Full Version : Programmatic access to current user's folder
Apr 15, 2009, 01:32 PM
Is it possible to open a file in the current user's home folder by doing something like (in C++):
ifstream fIn("/~/somefile.ext", ios::binary);
I know the code above does not work, but I wonder if there's anything like it available?
Or do you have to examine the HOME environment variable?
Apr 15, 2009, 02:13 PM
What language/framework are you using? In Cocoa you want to look at NSSearchPathForDirectoriesInDomains (http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/LowLevelFileMgmt/Tasks/LocatingDirectories.html) which can be used to return the path to the users home directory.
Apr 15, 2009, 02:24 PM
The reason "/~/somefile.ext" doesn't work is because "~" cannot have anything before it on Unix.
If you use "~/Library/Preferences/MyFile.ext", it will put the file in the current user's preferences folder. If you use "/~/Library/Preferences/MyFile.ext", it simply won't work.
This is supported on every API on OS X that takes a file path.