我如何以跨平台的方式,使用C ++找到用户的家庭目录? [英] How can I find the user's home dir in a cross platform manner, using C++?

查看:118
本文介绍了我如何以跨平台的方式,使用C ++找到用户的家庭目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



基本上,我正在寻找的是一个C ++的方式(例如在python中)

  os.path.expanduser(〜)


b
$ b

最便携的方法必须是在Unix上 getenv(HOME),并且连接 Windows上的getenv(HOMEDRIVE) getenv(HOMEPATH)


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

解决方案

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.

这篇关于我如何以跨平台的方式,使用C ++找到用户的家庭目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C/C++开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆