为什么我的 std::wofstream 写的是 ansi?

发布于 2024-08-24 00:49:25 字数 386 浏览 2 评论 0原文

我有宽字符串,我正在将其写入以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩于岁月 2024-08-31 00:49:25

请参阅页面底部的“社区内容”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).

归途 2024-08-31 00:49:25

当您使用输出宽流写入文件时,实际发生的情况是将宽字符转换为其他 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文