Is there an equivalent to setvbuf() in C or C++ to handle wide characters?

女生的网名这么多〃 提交于 2019-12-13 02:59:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!