Pebble Coding

ソフトウェアエンジニアによるIT技術、数学の備忘録

std::filesystemの便利さ

std::filesystem::pathは便利である。
文字コード変換機能が内蔵されており,macOS,windowsどちらも意図どおり動作する。
以下検証コードと実行結果である。 

    std::filesystem::path path1 = "あ";
    const unsigned char* a = (const unsigned char*)path1.string().c_str();
    printf("a: %02x %02x %02x %02x\n", *a, *(a+1), *(a+2), *(a+3));
    const wchar_t* b = path1.wstring().c_str();
    printf("b: %02x %02x %02x %02x\n", *b, *(b+1), *(b+2), *(b+3));
    
    std::filesystem::path path2 = L"あ";
    const unsigned char* c = (const unsigned char*)path2.string().c_str();
    
    printf("c: %02x %02x %02x %02x\n", *c, *(c+1), *(c+2), *(c+3));
    const wchar_t* d = path2.wstring().c_str();
    printf("d: %02x %02x %02x %02x\n", *d, *(d+1), *(d+2), *(d+3));
[Xcode15]
a: 00e3 0081 0082 0000
b: 3042 0000 0000 0000
c: 00e3 0081 0082 0000
d: 3042 0000 0000 0000
[Visual Studio 2022]
a: 0082 00a0 0000 0000
b: 3042 0000 0000 0000
c: 0082 00a0 0000 0000
d: 3042 0000 0000 0000

macOSはシステムのデフォルトがUTF8,日本語版WindowsはCP932となっているのがわかる。