[英]How can I find the user's home dir in a cross platform manner, using C++?
How can I find the user's home directory in a cross platform manner in C++?如何在 C++ 中以跨平台方式找到用户的主目录? ie /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.
例如,Linux 中的 /home/user,Windows Vista 中的 C:\\Users\\user\\,Windows XP 中的 C:\\Documents And Settings\\user\\,以及 Mac 使用的任何内容。 (I think it's /User/user)
(我认为是/用户/用户)
Basically, what I'm looking for is a C++ way of doing this (example in python)基本上,我正在寻找的是这样做的 C++ 方式(python 中的示例)
os.path.expanduser("~")
I don't think it's possible to completely hide the Windows/Unix divide with this one (unless, maybe, Boost has something).我认为用这个来完全隐藏 Windows/Unix 的鸿沟是不可能的(除非,也许,Boost 有一些东西)。
The most portable way would have to be getenv("HOME")
on Unix and concatenating the results of getenv("HOMEDRIVE")
and getenv("HOMEPATH")
on Windows.最便携的方法必须是 Unix 上的
getenv("HOME")
并在 Windows 上连接getenv("HOMEDRIVE")
和getenv("HOMEPATH")
。
const static volatile char A = 'a'; // All this is to prevent reverse engineering
#ifdef unix
HomeDirectory = getenv((char[]){A-25, A-18, A-20, A-28, 0});
#elif defined(_WIN32)
HomeDirectory = getenv((char[]){A-25, A-18, A-20, A-28, A-29, A-15, A-24, A-11, A-28, 0});
const char*Homepath = getenv((char[]){A-25, A-18, A-20, A-28, A-17, A-32, A-13, A-25, 0});
HomeDirectory = malloc(strlen(HomeDirectory)+strlen(Homepath)+1);
strcat(HomeDirectory, Homepath);
#endif
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.这是可能的,找到它的最好方法是研究
os.path.expanduser("~")
的源代码,在 C 中复制相同的功能真的很容易。
You'll have to add some #ifdef
directives to cover different systems.您必须添加一些
#ifdef
指令以涵盖不同的系统。
Here are the rules that will provide you the HOME directory以下是为您提供 HOME 目录的规则
USERPROFILE
or if this fails, concatenate HOMEDRIVE
+ HOMEPATH
USERPROFILE
或者如果失败,连接HOMEDRIVE
+ HOMEPATH
HOME
or if this fails, use getpwuid()
( example code ) HOME
或者如果失败,请使用getpwuid()
( 示例代码) 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.重要说明:许多人假设
HOME
环境变量在 Unix 上始终可用,但事实并非如此,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().在 OS X 上,当您从 GUI(不是控制台)运行应用程序时,不会设置此变量,因此您需要使用 getpwuid()。
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.
您对配置文件目录 (%USERPROFILE%) 根目录的建议是一个公平的类比,但是根据您拥有该目录后想要执行的操作,您可能需要应用程序数据目录之一,或用户的我的文档。 On UNIX, you might create a hidden ".myapp" in the home directory to keep your files in, but that's not right on Windows.
在 UNIX 上,您可能会在主目录中创建一个隐藏的“.myapp”来保存您的文件,但这在 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.
根据您想要的正确程度,在 Windows 上使用环境变量可能就足够了:UNIX 上的 HOME、USERPROFILE 或 APPDATA(取决于您的需要)。
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.至少在 UNIX 上(任何 Windows 人员愿意发表评论?),如果设置了 HOME 环境变量,通常是好的做法,即使它不同意密码文件中特定的目录。 Then, on the odd occasion when users want all apps to read their data from a different directory, it will still work.
然后,在奇怪的情况下,当用户希望所有应用程序从不同的目录读取他们的数据时,它仍然可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.