c++localizationc++20

How write UTF-8 filenames to MS Windows console with C++?


Take a look at this snippet (compiled on Windows 10 with MSVC 19.42 and /utf8 /std:c++20 options)

#include <iostream>
#include <locale>
#include <filesystem>
#include <fstream>

int main() {
   std::locale::global(std::locale("en_US.UTF-8"));
    
    std::filesystem::path p1(u8"ÄÖÜ.txt");
    std::ofstream(p1) << "ABC";
    std::cout << p1.filename() << "\n";
  
    std::filesystem::path p2(u8"файл.txt");
    std::ofstream(p2) << "ABC";
    std::cout << p2.filename() << "\n";
}

After the program has executed, I can see the following in my Windows console window:

D:\GitHubProjects\Cpp_Concurrency>dir D:\Temp\Folder
 Directory of D:\Temp\Folder

02/16/2025  05:42 PM    <DIR>          .
02/16/2025  05:42 PM    <DIR>          ..
02/16/2025  05:42 PM                 3 ÄÖÜ.txt
02/16/2025  05:42 PM                 3 файл.txt
               2 File(s)              6 bytes
               2 Dir(s)  884,556,988,416 bytes free

But the output of the program is this:

D:\>create_files.exe
"ÄÖÜ.txt"
"????.txt"

Any hints as to why the second filename does not get printed properly by the program?


Solution

  • The cmd.exe isn't expected UTF-8 encoding. Change the terminal to UTF-8 via chcp 65001 (the UTF-8 code page):

    C:\test>chcp
    Active code page: 437
    
    C:\test>test
    "ÄÖÜ.txt"
    "????.txt"
    
    C:\test>dir *.txt
     Volume in drive C is Windows
     Volume Serial Number is 8E84-82D1
    
     Directory of C:\test
    
    02/16/2025  09:17 AM                 3 ÄÖÜ.txt
    02/16/2025  09:17 AM                 3 файл.txt
                   2 File(s)              6 bytes
                   0 Dir(s)  1,858,843,865,088 bytes free
    
    C:\test>chcp 65001
    Active code page: 65001
    
    C:\test>test
    "ÄÖÜ.txt"
    "файл.txt"