Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使std::wofstream写入UTF-8?_C++_Logging_Utf 8_Widechar - Fatal编程技术网

C++ 如何使std::wofstream写入UTF-8?

C++ 如何使std::wofstream写入UTF-8?,c++,logging,utf-8,widechar,C++,Logging,Utf 8,Widechar,我正在将std::wclog重定向到一个文件,以便登录我的程序: std::wclog.rdbuf((new std::wofstream("C:\\path\\to\\file.log", std::ios::app))->rdbuf()); 记录通过写入std::wclog: std::wclog << "Schöne Grüße!" << std::endl; 应该是正确的 std::wclog << L"Schöne Grüße!" <

我正在将
std::wclog
重定向到一个文件,以便登录我的程序:

std::wclog.rdbuf((new std::wofstream("C:\\path\\to\\file.log", std::ios::app))->rdbuf());
记录通过写入
std::wclog

std::wclog << "Schöne Grüße!" << std::endl;
应该是正确的

std::wclog << L"Schöne Grüße!" << std::endl;
在哪里

因此,我想做的是使用
wofstring
wstring
写入
std::wclog
以将其放入文件中,并且该文件应该是UTF-8编码的(没有BOM)

openmode标志似乎没有提供此功能

因为它与openmode无关

代码转换(即字符编码)由流所使用的localecodevt方面执行。您可以使用转换为UTF-8的codecvt方面向ostream注入不同的语言环境


但我不知道这是否必要。我不知道Windows是如何运行的,但在正常的平台上,您只需将包含UTF-8的窄字符串写入clog,输出将是UTF-8,您不需要使用宽流。UTF-8是一种使用单八位字节(即窄字符)的多字节编码。

您只需使用UTF8文本即可,即:

std::wclog << u8"Schöne Grüße!" << std::endl;

std::wclog为什么要将窄字符写入宽字符流?我认为如果需要,需要使用UTF文本?那么语言环境呢?您需要为Unicode使用正确的类型和文字。Visual C++文字和类型。例如,`u8“hello”`是一个UTF-8编码的
char*
u“hello”
是一个
char16*
,而
u8“hello”s和
u“hello”s
返回
std::string
std::u16string
。一般来说,使用STL字符串类型要好得多。自2000年以来,SWINDOWS的核心是Unicode。这与Windows无关。当人们试图打开没有BOM的文件时(因此没有任何迹象表明文件不是ANSI),发现Windows假定他们使用用户输入的代码页作为
系统区域设置
BTW
w
的简单意思是宽,它没有指定编码。C++11为UTF-8、UTF-16和UTF-32添加了特定的类型,如,和Visual C++的@PanagiotisKanavos“自2000年以来Windows的核心是Unicode”。那么你怎么做OP想做的事情呢?@PanagiotisKanavos“Windows的核心是Unicode”不是真的,我希望是真的,但他们所做的只是增加了对宽字符的支持,它仍然是坏的。要在其核心完全支持Unicode,您需要有一个可以保存完整字符的wchar,即32位,但在Windows中,wchar是16位。@AndersK。首先,您谈论的是UTF-32,而Unicode指的是UTF-16。UTF 32是一个相对的新手,但是如果您使用正确的类型,您可以在C++中使用UTF32。
std::wclog << _(L"Best regards") << std::endl;
#define _(X) i18n::translate(X)

class i18n {
public:
    static std::wstring translate(const std::wstring&);
}
std::wclog << u8"Schöne Grüße!" << std::endl;
std::wclog << "Schöne Grüße!" << std::endl << u8"Schöne Grüße!" <<