Why does my std::wofstream write ansi?

本秂侑毒 提交于 2019-12-24 13:33:59

问题


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();

回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/2380307/why-does-my-stdwofstream-write-ansi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!