Combining Two Paths into a Single Path - C++ File Stream

C++ examples for File Stream:Directory

Description

Combining Two Paths into a Single Path

Demo Code

#include <iostream>
#include <string>

using std::string;

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

   char sep = '/';
   string tmp = p1;//from  w w  w .  j a v  a 2  s. c  o  m

#ifdef _WIN32
  sep = '\\';
#endif

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

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

   string path = "c:/abc/def";

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

Result


Related Tutorials