Reputation: 1532
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?
Upvotes: 1
Views: 76
Reputation: 81
Windows console is not defaulted to unicode codepage, but it can be changed from the code.
Option 1:
int main()
{
#ifdef _WIN32 // console UTF-8
system("chcp 65001");
#endif
}
Option 2:
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <locale.h>
#endif // _WIN32
int main()
{
#ifdef _WIN32 // console UTF-8
setlocale(LC_CTYPE, ".UTF8");
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP (CP_UTF8);
#endif
}
Upvotes: 1
Reputation: 178031
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"
Upvotes: 2