Appending to boost::filesystem::path

后端 未结 5 1313
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 00:53

I have a certain boost::filesystem::path in hand and I\'d like to append a string (or path) to it.

boost::filesystem::path p(\"c:\\\\dir\");
p.appen         


        
5条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 01:40

    #include 
    #include 
    #include 
    
    
    int main() {
      boost::filesystem::path p (__FILE__);
    
      std::string new_filename = p.leaf() + ".foo";
      p.remove_leaf() /= new_filename;
      std::cout << p << '\n';
    
      return 0;
    }
    

    Tested with 1.37, but leaf and remove_leaf are also documented in 1.35. You'll need to test whether the last component of p is a filename first, if it might not be.

提交回复
热议问题