[英]Remove Filename From Filepath c++
所以我有这段代码可以从文件路径中删除文件名,但是我该如何做相反的事情,即删除除文件名之外的所有内容?
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);
};
所以如果我有这个文件路径"C:\\Testdir\\file.exe"
它将变成"file.exe"
。
除了文件名之外,我将如何获得所有内容?
使用<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\\"
我找到了解决方案
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.