Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending to boost::filesystem::path

Tags:

c++

boost

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?

like image 918
Zack Avatar asked Mar 07 '10 14:03

Zack


People also ask

What is boost :: filesystem :: path?

boost::filesystem::path is the central class in Boost. Filesystem for representing and processing paths. Definitions can be found in the namespace boost::filesystem and in the header file boost/filesystem. hpp . Paths can be built by passing a string to the constructor of boost::filesystem::path (see Example 35.1).


1 Answers

If it's really just the file name extension you want to change then you are probably better off writing:

p.replace_extension(".foo"); 

for most other file path operations you can use the operators /= and / allowing to concatenate parts of a name. For instance

boost::filesystem::path p("c:\\dir"); p /= "subdir"; 

will refer to c:\dir\subdir.

like image 79
hkaiser Avatar answered Oct 07 '22 03:10

hkaiser