Extracting a Path from a Full Path and Filename - C++ File Stream

C++ examples for File Stream:Directory

Description

Extracting a Path from a Full Path and Filename

Demo Code

#include <iostream>
#include <string>

using std::string;

string getPathName(const string& s) {

   char sep = '/';

#ifdef _WIN32//from   www. j  a v  a2 s.c o m
   sep = '\\';
#endif

   size_t i = s.rfind(sep, s.length());
   if (i != string::npos) {
      return(s.substr(0, i));
   }
   return("");
}

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

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

   std::cout << "The path name is \"" << getPathName(path) << "\"\n";
}

Result


Related Tutorials