Home >Backend Development >C++ >How to Achieve Cross-Platform UTF-8 Output with std::cout on Windows?
Cross-Platform UTF-8 Output on Windows with std::cout
When developing cross-platform applications in C where strings are encoded in UTF-8, it is crucial to ensure that they are handled correctly across different operating systems. On Unix systems, std::cout expects UTF-8-encoded 8-bit strings without issue. However, on Windows, it defaults to Latin-1 or similar non-Unicode formats.
To address this issue, the code attempts to use _setmode() to force std::cout to interpret 8-bit strings as UTF-8 on Windows. However, this triggers an assertion error in Visual Studio 2015.
To resolve this, a more comprehensive approach is required. The code shown below combines insights from various sources:
<code class="cpp">#include <string> #include <iostream> #include <Windows.h> #include <cstdio> int main() { // Set console code page to UTF-8 SetConsoleOutputCP(CP_UTF8); // Enable stream buffering to prevent byte-by-byte output setvbuf(stdout, nullptr, _IOFBF, 1000); std::string test = u8"Greek: αβγδ; German: Übergrößenträger"; std::cout << test << std::endl; return 0; }</code>
Firstly, the SetConsoleOutputCP() function is used to set the code page of the console to UTF-8, indicating that the received byte stream should be interpreted as UTF-8.
Next, buffering is enabled for std::cout using setvbuf(). This is because the default behavior of Visual Studio's STL implementation is to pass individual bytes to the underlying file, causing problems with UTF-8 byte sequences. Buffering prevents this issue and improves performance. Flushing the stream periodically with std::endl, as shown in this example, is also recommended.
Finally, note that for non-ASCII Unicode characters to be displayed correctly, the Windows console must use a TrueType font. This is the default in Windows 10, but on Windows 7 or earlier, the user may need to manually change it to a font such as Consolas.
The above is the detailed content of How to Achieve Cross-Platform UTF-8 Output with std::cout on Windows?. For more information, please follow other related articles on the PHP Chinese website!