Appending to boost::filesystem::path

boost, c++

Solution

#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 << '\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.

Problem

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 `InputIterator`s. My solution so far is to do the following: ``` boost::filesystem::path p2(std::string(p.string()).append(".foo")); ``` Am I missing something?

Original source