Let's say that filesystem::current_path
will return the path:
/tmp/1567519419.773012
But I want a trailing separator. If seems like all I should have to do would be:
filesystem::current_path() / filesystem::path()
This works on gcc giving me:
/tmp/1567519419.773012/
But on visual-studio, while the filesystem::current_path
also gives me the absolute path without a trailing seperator, dividing by filesystem::path()
has no effect. The resulting path is still the absolute path without a trailing separator.
I'd like code that is cross platform compatible, and I'd like to avoid having to detect whether the current path already has a trailing separator.
Is there anything available to me?
I'm not familiar enough with filesystem
to know which compiler is correct (possibly both are, if there is implementation-defined behavior involved). However, the following should work on all platforms that correctly implement filesystem
:
#include <iostream>
#include <filesystem>
int main() {
auto foo = std::filesystem::current_path();
foo += foo.preferred_separator; // A static data member of std::filesystem::path
// The lexically normal form eliminates doubled separators
std::cout << foo.lexically_normal().string() << '\n';
}