Combining Two Paths into a Single Path


You have two paths and you have to combine them into a single path. You may have something like /usr/home/ryan as a first path, and utils/compilers as the second, and wish to get /usr/home/ryan/utils/compilers, without having to worry whether or not the first path ends with a path separator.


Treat the paths as strings and use the append operator, operator+=, to compose a full path out of partial paths. See Example 10-26.

Example 10-26. Combining paths


using std::string;

string pathAppend(const string& p1, const string& p2) {

 char sep = '/';
 string tmp = p1;

#ifdef _WIN32
 sep = '\';

 if (p1[p1.length( )] != sep) { // Need to add a
 tmp += sep; // path separator
 return(tmp + p2);
 return(p1 + p2);

int main(int argc, char** argv) {

 string path = argv[1];

 std::cout << "Appending somedir\anotherdir is ""
 << pathAppend(path, "somedir\anotherdir") << ""



The code in Example 10-26 uses strings that represent paths, but there's no additional checking on the path class for validity and the paths used are only as portable as the values they contain. If, for example, these paths are retrieved from the user, you don't know if they're using the right OS-specific format, or if they contain illegal characters.

For many other recipes in this chapter I have included examples that use the Boost Filesystem library, and when working with paths, this approach has lots of benefits. As I discussed in Recipe 10.7, the Boost Filesystem library contains a path class that is a portable representation of a path to a file or directory. The operations in the Filesystem library mostly work with path objects, and as such, the path class can handle path composition from an absolute base and a relative path. (See Example 10-27.)

Example 10-27. Combining paths with Boost


using namespace std;
using namespace boost::filesystem;

int main(int argc, char** argv) {

 // Parameter checking...

 try {
 // Compose a path from the two args
 path p1 = complete(path(argv[2], native),
 path(argv[1], native));
 cout << p1.string( ) << endl;

 // Create a path with a base of the current dir
 path p2 = system_complete(path(argv[2], native));
 cout << p2.string( ) << endl;
 catch (exception& e) {
 cerr << e.what( ) << endl;


The output of the program in Example 10-27 might look like this:

D:srcccbc10>binMakePathBoost.exe d:	emp someotherdir


D:srcccbc10>binMakePathBoost.exe d:	emp c:WINDOWSsystem32

What you can see here is that complete and system_complete merge paths when possible, or return the absolute path when merging paths makes no sense. For example, in the first output, the first argument given to the program is an absolute directory and the second is a relative directory. complete merges them together and produces a single, absolute path. The first argument to complete is the relative path, and the second is the absolute path, and if the first argument is already an absolute path, the second argument is ignored. That's why in the second output you can see that the argument "d: emp" is ignored since the second argument I give is already an absolute path.

system_complete only takes a single argument (the relative path in this case) and appends it to the current working directory to produce another absolute path. Again, if you give it a path that is already absolute, it ignores the current working directory and simply returns the absolute path you gave it.

These paths are not reconciled with the filesystem though. You have to explicitly test to see if a path object represents a valid filesystem path. For example, to check if one of these paths exists, you can use the exists function on a path:

path p1 = complete(path(argv[2], native),
 path(argv[1], native));
if (exists(p1)) {
 // ...

There are many more functions you can use to get information about a path: is_dir-ectory, is_empty, file_size, last_write_time, and so on. See the Boost Filesystem library documentation at for more information.

See Also

Recipe 10.7

Building C++ Applications

Code Organization


Strings and Text

Dates and Times

Managing Data with Containers



Exceptions and Safety

Streams and Files

Science and Mathematics






C++ Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2006
Pages: 241 © 2008-2020.
If you may any questions please contact us: