展开在其路径中具有环境变量的文件名 [英] expand file names that have environment variables in their path
问题描述
将
$ b
What's the best way to expand
${MyPath}/filename.txt to /home/user/filename.txt
或
%MyPath%/filename.txt to c:\Documents and settings\user\filename.txt
与遍历路径字符串直接查找环境变量?
我看到wxWidgets有一个 wxExpandEnvVars 函数。我不能在这种情况下使用wxWidgets,所以我希望找到一个boost :: filesystem等效或类似的。我只是使用主目录作为例子,我正在寻找通用的路径扩展。
with out traversing the path string looking for environement variables directly? I see that wxWidgets has a wxExpandEnvVars function. I can't use wxWidgets in this case, so I was hoping to find a boost::filesystem equivalent or similar. I am only using the home directory as an example, I am looking for general purpose path expansion.
推荐答案
对于UNIX最少POSIX)系统,请查看 wordexp :
For UNIX (or at least POSIX) systems, have a look at wordexp:
#include <iostream>
#include <wordexp.h>
using namespace std;
int main() {
wordexp_t p;
char** w;
wordexp( "$HOME/bin", &p, 0 );
w = p.we_wordv;
for (size_t i=0; i<p.we_wordc;i++ ) cout << w[i] << endl;
wordfree( &p );
return 0;
}
看起来它甚至会做类似glob的扩展对您的特殊情况有用)。
It seems it will even do glob-like expansions (which may or may not be useful for your particular situation).
这篇关于展开在其路径中具有环境变量的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!