How can I find the user's home dir in a cross

2019-01-21 13:41发布

How can I find the user's home directory in a cross platform manner in C++? i.e. /home/user in Linux, C:\Users\user\ on Windows Vista, C:\Documents And Settings\user\ on Windows XP, and whatever it is that Macs use. (I think it's /User/user)

Basically, what I'm looking for is a C++ way of doing this (example in python)

os.path.expanduser("~")

3条回答
走好不送
2楼-- · 2019-01-21 14:25

The home directory isn't really a cross-platform concept. Your suggestion of the root of the profile directory (%USERPROFILE%) is a fair analogy, but depending what you want to do once you have the directory, you might want one of the Application Data directories, or the user's My Documents. On UNIX, you might create a hidden ".myapp" in the home directory to keep your files in, but that's not right on Windows.

Your best bet is to write specific code for each platform, to get at the directory you want in each case. Depending how correct you want to be, it might be enough to use env vars: HOME on UNIX, USERPROFILE or APPDATA (depending what you need) on Windows.

On UNIX at least (any Windows folks care to comment?), it's usually good practice to use the HOME environment variable if it's set, even if it disagrees with the directory specific in the password file. Then, on the odd occasion when users want all apps to read their data from a different directory, it will still work.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-21 14:44

This is possible, and the best way to find it is to study the source code of os.path.expanduser("~"), it is really easy to replicate the same functionality in C.

You'll have to add some #ifdef directives to cover different systems.

Here are the rules that will provide you the HOME directory

  • Windows: env USERPROFILE or if this fails, concatenate HOMEDRIVE+HOMEPATH
  • Linux, Unix and OS X: env HOME or if this fails, use getpwuid() (example code)

Important remark: many people are assuming that HOME environment variable is always available on Unix but this is not true, one good example would be OS X.

On OS X when you run an application from GUI (not console) this will not have this variable set so you need to use the getpwuid().

查看更多
唯我独甜
4楼-- · 2019-01-21 14:46

I don't think it's possible to completely hide the Windows/Unix divide with this one (unless, maybe, Boost has something).

The most portable way would have to be getenv("HOME") on Unix and concatenating the results of getenv("HOMEDRIVE") and getenv("HOMEPATH") on Windows.

查看更多
登录 后发表回答