为什么我的std :: wofstream写ansi? [英] Why does my std::wofstream write ansi?
问题描述
我有宽字符串,并且正在将其写入以二进制模式打开的wofstream中.当我查看结果文件时,它丢失了所有其他字节.
I've got wide string and I'm writing it to a wofstream that I opened in out|binary mode. When I look in the resultant file, it's missing every other byte.
我期望当我在Visual Studio中使用二进制编辑器打开文件时,我会看到其他字节为零,但是我看不到零.
I was expecting that when I opened the file in visual studio with the binary editor that I'd see every other byte as a zero, but I'm not seeing the zeros.
你知道我想念什么吗?
谢谢.
代码是这样的:
CAtlStringW data = L"some data";
wofstream stream("c:\hello.txt", ios_base:out|ios_base:binary);
stream.write( data.GetBuffer(), data.GetLength() );
stream.close();
推荐答案
使用输出宽流向文件写入时,实际发生的是将宽字符转换为其他8位编码.
When you write to file using output wide stream, what actually happens is that it converts the wide characters to other 8-bit encoding.
如果您使用的是UTF-8语言环境,则会将宽字符串转换为UTF-8编码的文本(但MSVC不提供UTF-8语言环境),因此通常它将尝试转换为某些代码页(如cp1251或ASCII)
If you were using UTF-8 locale it would convert wide strings to UTF-8 encoded text (but MSVC does not provides UTF-8 locales) so generally it would try to convert to some code-page like cp1251 or to ASCII.
这篇关于为什么我的std :: wofstream写ansi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!