So I have this code that removes the filename from the filepath, but how do I do the opposite ie remove everything except the filename?
sd::string filename = "C:\\Testdir\\file.exe";
const size_t last_slash_idx = filepath_modify.rfind('\\');
if (std::string::npos != last_slash_idx) {
filepath_modify = filepath_modify.substr(0, last_slash_idx);
};
So if I have this filepath "C:\\Testdir\\file.exe"
it will become "file.exe"
.
How would I get everything except the filename?
Use the <filesystem>
header:
#include <filesystem>
#include <string>
std::filesystem::path filepath{"C:\\Testdir\\file.exe"};
std::string filename = filepath.filename();
//"file.exe"
std::string folderpath = filepath.parent_path();
//"C:\\Testdir\\"
I found a solution
std::string filepath = "C:\\Testdir\\file.exe";
const size_t last_slash_idx = filepath.rfind('\\', filepath_modify.length());
if (std::string::npos != last_slash_idx) {
filepath =filepath.substr(last_slash_idx + 1, filepath.length() - last_slash_idx);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.