[英]Remove Filename From Filepath c++
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"
.所以如果我有这个文件路径
"C:\\Testdir\\file.exe"
它将变成"file.exe"
。
How would I get everything except the filename?除了文件名之外,我将如何获得所有内容?
Use the <filesystem>
header:使用
<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);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.