Remove dot prefix "./" from std::filesystem::path

Viewed 293

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). 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?
}
1 Answers

Apparently the problem could be solved with std::filesystem::relative():

#include <iostream>
#include <filesystem>

namespace filesystem = std::filesystem;

int main() {
    auto path = filesystem::path{"./x"};
    std::cout << path << std::endl;
    std::cout << filesystem::relative(path, "./") << std::endl; // 
}

Produces

"./x"
"x"
Related