For example I have the string "root/data/home/file1.txt"
I would like to get "root/data/home"
Is there a convenient function in C++ that allows me to do this or should I code it myself?
You can do basic string manipulation, i.e.
std::string path = "root/data/home/file1.txt";
// no error checking here
std::string prefix = path.substr(0, path.find_last_of('/'));
or take a third option like Boost.Filesystem:
namespace fs = boost::filesystem;
fs::path path = "root/data/home/file1.txt";
fs::path prefix = path.parent_path();
If you're on a POSIX system, try dirname(3).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With