展开在其路径中具有环境变量的文件名 [英] expand file names that have environment variables in their path

查看:172
本文介绍了展开在其路径中具有环境变量的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆