Home > Net >  std::filesystem::weakly_canonical() does not seem to work with drive letters
std::filesystem::weakly_canonical() does not seem to work with drive letters

Time:10-11

The following code:

#include <iostream>
#include <filesystem>

int main()
{
    std::filesystem::path path("C:");
    std::filesystem::path canonicalPath = std::filesystem::weakly_canonical(path);
    std::cout << canonicalPath.string() << std::endl;
}

produces the output: C:\Users\andy\source\repos\ConsoleApplication8\ConsoleApplication8 using Visual Studio 2019 X64. On x86-64 gcc it produces what I expect: C: according to godbolt.org.

CodePudding user response:

Windows and Linux have different rules when it comes to paths.

For example, on Windows using the drive-letter and colon only means the current directory on the specified drive. This behavior is inherited from DOS.

When you build using GCC on the compiler explorer, it uses a virtual Linux environment, and Linux doesn't have drive letters. Which means that C: isn't a path in Linux, but rather a file-name.

  • Related