问题
The C++ std::setvbuf function enables the regulation of a file stream by various buffering options. That stream can only process plain chars though. Is there an equivalent version for wide characters (wchar_t) available?
回答1:
setvbuf works in Windows 8+ to enable writing UTF8 in console window. Reading UTF8 is still not supported as of Windows 10 build 1803.
In Visual Studio you can use compiler specific _setmode to read/write UTF16 in console window. But this may not be an option in other compilers (MinGW-32 didn't support it last time I checked). The only other option would be to write your stream functions based on WriteConsoleW.
Note that the console window may not support printing Unicode code points above 0xFFFF unless you change the console font to appropriate font with SetCurrentConsoleFontEx, such as "MS Gothic" (which still doesn't handle many code points)
#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
_setmode(_fileno(stdin), _O_U16TEXT);
std::wcout << L"UTF16 English ελληνικά\n";
std::wstring utf16;
std::wcin >> utf16;
std::wcout << utf16 << "\n";
return 0;
}
来源:https://stackoverflow.com/questions/52903141/is-there-an-equivalent-to-setvbuf-in-c-or-c-to-handle-wide-characters