When working with UTF-8-encoded strings in a cross-platform C application, issues can arise when attempting to print them to std::cout on Windows systems. By default, std::cout expects 8-bit strings to be in Latin-1 or a similar non-Unicode format, leading to garbled output for UTF-8 strings.
To address this problem on Windows, several approaches can be taken:
Setting Console Code Page to UTF-8:
SetConsoleOutputCP(CP_UTF8);
This function tells the Windows console to interpret the byte stream it receives as UTF-8.
Resolving Stream Buffering Issue in Visual Studio STL:
setvbuf(stdout, nullptr, _IOFBF, 1000);
This workaround enables buffering for the stream, preventing the STL code from sending UTF-8 byte sequences as individual bytes to the console.
Ensuring TrueType Font is Used:
TrueType fonts support Unicode characters, while raster fonts ignore console code pages. Windows 10 uses Consolas as the default font, but in Windows 7, users may need to manually change to a TrueType font to display non-ASCII Unicode characters correctly.
The above is the detailed content of How to Achieve Correct UTF-8 Output with std::cout on Windows?. For more information, please follow other related articles on the PHP Chinese website!