[英]Remove dot prefix "./" from std::filesystem::path
Is there a simple way to strip away the starting ./
of a path.有没有一种简单的方法可以去除路径的起始
./
。 For example: I have a path ./x/y
and i want to convert it to x/y
(without the first dot and slash).例如:我有一个路径
./x/y
,我想将它转换为x/y
(没有第一个点和斜杠)。 Is there a standard way of doing it?有标准的做法吗?
#include <iostream>
#include <filesystem>
namespace filesystem = std::filesystem;
int main() {
auto path = filesystem::path{"./x/y"};
std::cout << path << std::endl;
std::cout << ???? << std::endl; // How do I do this?
}
Apparently the problem could be solved with std::filesystem::relative()
:显然这个问题可以用
std::filesystem::relative()
解决:
#include <iostream>
#include <filesystem>
namespace filesystem = std::filesystem;
int main() {
auto path = filesystem::path{"./x"};
filesystem::current_path("./");
std::cout << path << std::endl;
std::cout << filesystem::relative(path, "./") << std::endl; //
}
Produces生产
"./x"
"x"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.