如何使用 C++ 以跨平台方式找到用户的主目录?

发布于 2024-08-27 14:02:02 字数 256 浏览 6 评论 0原文

如何在C++中以跨平台的方式找到用户的主目录?即 Linux 中的 /home/user、Windows Vista 中的 C:\Users\user\、Windows XP 中的 C:\Documents And Settings\user\ 以及 Mac 使用的任何内容。 (我认为它是 /User/user)

基本上,我正在寻找的是一种 C++ 方法(Python 中的示例)

os.path.expanduser("~")

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("~")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2024-09-03 14:02:02

我认为不可能用这个来完全隐藏 Windows/Unix 的鸿沟(除非 Boost 可能有一些东西)。

最可移植的方法必须是 Unix 上的 getenv("HOME") 并将 getenv("HOMEDRIVE")getenv("HOMEPATH" 的结果连接起来)(在 Windows 上)。

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.

北音执念 2024-09-03 14:02:02

这个是可能的,找到它的最好方法就是研究os.path.expanduser("~")的源代码,复制起来真的很容易C 中具有相同的功能。

您必须添加一些 #ifdef 指令来覆盖不同的系统。

以下规则将为您提供

  • Windows HOME 目录: env USERPROFILE 或者如果失败,请连接 HOMEDRIVE+HOMEPATH
  • Linux、Unix 和 OS X: env HOME 或者如果失败,请使用 getpwuid()示例代码)

重要提示:许多人假设 HOME 环境变量在 Unix 上始终可用,但事实并非如此,OS X 就是一个很好的例子

。 OS X 当您从 GUI(而不是控制台)运行应用程序时,不会设置此变量,因此您需要使用 getpwuid()。

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().

若有似无的小暗淡 2024-09-03 14:02:02

主目录并不是真正的跨平台概念。您对配置文件目录根目录 (%USERPROFILE%) 的建议是一个合理的类比,但根据您拥有该目录后想要执行的操作,您可能需要应用程序数据目录之一或用户的“我的文档”。在 UNIX 上,您可以在主目录中创建一个隐藏的“.myapp”来保存文件,但这在 Windows 上是不正确的。

最好的选择是为每个平台编写特定的代码,以获取每种情况下所需的目录。根据您想要的正确程度,使用环境变量可能就足够了:UNIX 上的 HOME、Windows 上的 USERPROFILE 或 APPDATA(取决于您的需要)。

至少在 UNIX 上(任何 Windows 人员愿意发表评论吗?),如果设置了 HOME 环境变量,通常最好使用它,即使它与密码文件中特定的目录不一致。然后,在奇怪的情况下,当用户希望所有应用程序从不同的目录读取数据时,它仍然可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文