简体   繁体   English

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

[英]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 目录的规则

  • Windows: env USERPROFILE or if this fails, concatenate HOMEDRIVE + HOMEPATH Windows:env USERPROFILE或者如果失败,连接HOMEDRIVE + HOMEPATH
  • Linux, Unix and OS X: env HOME or if this fails, use getpwuid() ( example code ) Linux、Unix 和 OS X:env 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.

相关问题 如何在C ++跨平台中获取用户的组列表? - How to get a user's group list in c++ cross platform? 如何编写处理信号的跨平台c ++? - How can I write cross platform c++ that handles signals? 如何以跨平台方式在C / C ++中测试计算机的实际一般性能? - How to test a computer real general performance within C/C++ in a cross platform manner? C++ - 如何以独立于平台、线程安全的方式以用户首选的日期/时间语言环境格式格式化文件的最后修改日期和时间 - C++ - How to format a file's last modified date and time in the user's preferred date/time locale format in a platform-independent, thread-safe manner 我可以使用Visual C ++开发跨平台桌面应用程序吗? - Can I develop Cross-Platform Desktop Applications using Visual C++? 如何使我的C ++代码跨平台兼容? - How can I make my C++ code cross platform capable? 如何在跨平台的C ++应用程序中嵌入PDF查看器? - How can I embed a PDF viewer in a cross-platform C++ application? 如何在Mac上编写跨平台的C ++程序? - How can I write cross-platform c++ programs on my mac? 如何在C ++ Builder XE5跨平台中获取TAlphaColor的RGB组件? - How can I get the RGB components of a TAlphaColor in C++ Builder XE5 cross-platform? 如何在iOS平台上使用C ++实现GameCenter - How can I implement GameCenter using c++ on ios platform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM