簡體   English   中英

使用boost :: filesystem擴展用戶路徑

[英]Expanding user path with 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;
    }
  }

另外,看看@WhiteViking提出的類似問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM