Skip to content

Support UTF-8 locale for std::filesystem::path #534

@huangqinjin

Description

@huangqinjin
#include <filesystem>
#include <iostream>
#include <clocale>
#include <windows.h>

int main(int argc, char* argv[])
{
	SetConsoleOutputCP(CP_UTF8);
	std::setlocale(LC_CTYPE, ".65001");
	std::cout << std::filesystem::path(L"中文").string() << std::endl;
}

The code above compiled with llvm-mingw results in error at runtime:

libc++abi: terminating due to uncaught exception of type std::__1::__fs::filesystem::filesystem_error: filesystem error: in __wide_to_char: Illegal byte sequence

With clang-cl, it correctly prints the path to Console. Checked MS STL source code, it retrieves UTF-8 code page from ___lc_codepage_func.
https://github.com/microsoft/STL/blob/cbe2ee99caf122555a305d18f69e57c75b3fe5ec/stl/src/filesystem.cpp#L232-L244

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions