Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c++ - Appending to boost::filesystem::path

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.append(".foo"); // should result in p pointing to c:dir.foo

The only overload boost::filesystem::path has of append wants two InputIterators.

My solution so far is to do the following:

boost::filesystem::path p2(std::string(p.string()).append(".foo"));

Am I missing something?

question from:https://stackoverflow.com/questions/2396548/appending-to-boostfilesystempath

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>


int main() {
  boost::filesystem::path p (__FILE__);

  std::string new_filename = p.leaf() + ".foo";
  p.remove_leaf() /= new_filename;
  std::cout << p << '
';

  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...