Although that wouldn't work on a system that has different directory separators than '/', e.g a Windows system which uses : and \, or one of them systems that uses : only.
But the idea is good, and it's definitely simple.
--
Mats
Printable View
Here's a more portable version:
Code:std::string dirname(const std::string & path, const std::string & sep)
{
std::string dir = path.substr(0, path.find_last_of(sep));
return ((dir != path) ? dir : std::string("."));
}