2015-10-19 4 views
5

boost::filesystemには、Pythonで提供されているos.path.expanduserの機能と同様に、ユーザのホームディレクトリシンボル(Unixでは~)で始まるパスを展開する機能がありますか?boost :: filesystemでユーザパスを拡張する

+0

http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#canonicalを使用しようとしましたか? – Hamdor

+0

@Hamdor 'canonical(path("〜/ test.txt "))'のようなものを試しましたが、うまくいかなかった。不適切な使用法ですか? – Daniel

+0

私はそこに疑いがあります。 http://stackoverflow.com/questions/4891006/how-to-create-a-folder-in-the-home-directoryも参照してください。 – WhiteViking

答えて

4

しかし、あなたがこのような何かを行うことによって、それを実装することができます。また

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によって提案similar questionを見てみましょう。

関連する問題