c++ how to remove filename from path string

c++, filesystems, string

Solution

The easiest way is to use `find_last_of` member function of `std::string`

string s1("../somepath/somemorepath/somefile.ext");
string s2("..\\somepath\\somemorepath\\somefile.ext");
cout << s1.substr(0, s1.find_last_of("\\/")) << endl;
cout << s2.substr(0, s2.find_last_of("\\/")) << endl;

This solution works with both forward and back slashes.

Problem

I have ``` const char *pathname = "..\somepath\somemorepath\somefile.ext"; ``` how to transform that into ``` "..\somepath\somemorepath" ``` ?

Original source

Related problems