Dan*_*iel 9 c++ boost-filesystem
是否有功能boost::filesystem扩展以用户主目录符号开头的路径(~在Unix上),类似于Python中提供的os.path.expanduser函数?
没有.
但您可以通过执行以下操作来实现它:
namespace bfs = boost::filesystem;
using std;
bfs::path expand (bfs::path in) {
if (in.size () < 1) return in;
const char * home = getenv ("HOME");
if (home == NULL) {
cerr << "error: HOME variable not set." << endl;
throw std::invalid_argument ("error: HOME environment variable not set.");
}
string s = in.c_str ();
if (s[0] == '~') {
s = string(home) + s.substr (1, s.size () - 1);
return bfs::path (s);
} else {
return in;
}
}
Run Code Online (Sandbox Code Playgroud)
另外,看看@WhiteViking提出的类似问题.