为什么我的 std::wofstream 写的是 ansi?
我有宽字符串,我正在将其写入以 out|binary 模式打开的 wofstream 中。当我查看生成的文件时,它丢失了所有其他字节。
我期望当我使用二进制编辑器在 Visual Studio 中打开文件时,我会看到所有其他字节都为零,但我没有看到零。
你知道我错过了什么吗?
谢谢。
代码是这样的:
CAtlStringW data = L"some data";
wofstream stream("c:\hello.txt", ios_base:out|ios_base:binary);
stream.write( data.GetBuffer(), data.GetLength() );
stream.close();
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.
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.
Do you know what I'm missing?
Thanks.
The code is something like this:
CAtlStringW data = L"some data";
wofstream stream("c:\hello.txt", ios_base:out|ios_base:binary);
stream.write( data.GetBuffer(), data.GetLength() );
stream.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅页面底部的“社区内容”http ://msdn.microsoft.com/en-us/library/f1d6b0fk(VS.80).aspx。简而言之,您必须使用 pubsetbuf() 为您的流使用基于 wchar_t 的内部缓冲区(而不是基于 char 的)。
see the "Community Content" at the bottom of the page http://msdn.microsoft.com/en-us/library/f1d6b0fk(VS.80).aspx. In short, you have to use pubsetbuf() to use a wchar_t based internal buffer for your stream (instead of a char based).
当您使用输出宽流写入文件时,实际发生的情况是将宽字符转换为其他 8 位编码。
如果您使用 UTF-8 语言环境,它会将宽字符串转换为 UTF-8 编码文本(但 MSVC 不提供 UTF-8 语言环境),因此通常它会尝试转换为某些代码页,例如 cp1251 或 ASCII。
When you write to file using output wide stream, what actually happens is that it converts the wide characters to other 8-bit encoding.
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.