在C或C ++中是否有等效于setvbuf()的函数来处理宽字符? [英] Is there an equivalent to setvbuf() in C or C++ to handle wide characters?
问题描述
C ++ std :: setvbuf
函数可通过各种缓冲选项来调节文件流。该流只能处理普通的 char
s。是否有等效的宽字符( wchar_t
)版本?
The C++ std::setvbuf
function enables the regulation of a file stream by various buffering options. That stream can only process plain char
s though. Is there an equivalent version for wide characters (wchar_t
) available?
推荐答案
setvbuf
在Windows 8+中可以在控制台窗口中写入UTF8。从Windows 10内部版本1803开始,仍不支持读取UTF8。
setvbuf
works in Windows 8+ to enable writing UTF8 in console window. Reading UTF8 is still not supported as of Windows 10 build 1803.
在Visual Studio中,您可以使用特定于编译器的 _setmode
在控制台窗口中读取/写入UTF16。但这可能不是其他编译器的选项(我上次检查时MinGW-32不支持此功能)。唯一的其他选择是基于 WriteConsoleW
编写流函数。
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
.
请注意,控制台窗口可能不会支持打印 0xFFFF
以上的Unicode代码点,除非您使用 SetCurrentConsoleFontEx
将控制台字体更改为适当的字体,例如 MS Gothic
(仍然无法处理很多代码点)
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;
}
这篇关于在C或C ++中是否有等效于setvbuf()的函数来处理宽字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!