Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Streaming a wstring to a wofstream

63 views
Skip to first unread message

Edward Diener

unread,
Jul 25, 2008, 2:08:57 PM7/25/08
to
Is there ever any case in which streaming a wstring to a wofstream
should fail to write all the characters in the wstring to the file ? In
code, built with VC9 in Windows, I have:

std::wstring astring;
// Code that fills the wstring with about 210,000 wide characters.
// I can verify the size of this in the debugger.
std::wofstream wofs("somefile",std::ios_base::out | std::ios_base::binary);
wofs << astring;
// wofs gets closed by going out of scope

When I check the file written it is truncated after about 93000 bytes or
half that many wide characters.

In other cases when streaming a much smaller ( 50 characters or so )
wstring using a wofstream the output is correct.

Ideas ? Or just a possible bug in VC9 which I need to duplicate and
presenrt to Microsoft ?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Oncaphillis

unread,
Jul 25, 2008, 8:28:26 PM7/25/08
to
Edward Diener wrote:

> Is there ever any case in which streaming a wstring to a wofstream
> should fail to write all the characters in the wstring to the file ? In
> code, built with VC9 in Windows, I have:
>
> std::wstring astring;
> // Code that fills the wstring with about 210,000 wide characters.
> // I can verify the size of this in the debugger.
> std::wofstream wofs("somefile",std::ios_base::out |
> std::ios_base::binary); wofs << astring;

Did you check the streams internal state after writing ?
i.e.

if(!wofs) {
std::cerr << "somethings fishy" << std::endl;
}

> // wofs gets closed by going out of scope
>
> When I check the file written it is truncated after about 93000 bytes or
> half that many wide characters.

Are you *really* writing wchar_t to the underlying file i.e. uint(16|32) ?
Or is the wofstream only a front end for a byte based encoding like
utf8 or a JIS variant or..or..or ?

Is the stream properly imbued ? If so to which locale ?
If you use a codecvt facet that does not support the
utf-16/32 code (I assume your wchar_t is meant to
hold unicode) writing might be canceled although
the g++ implementation throws an exception in that case.

(Hmm -- makes me wonder, isn't it forbidden for iostream operations
to throw exceptions ?. Or was there some kind of flags to manipulate
this behavior ?)

> In other cases when streaming a much smaller ( 50 characters or so )
> wstring using a wofstream the output is correct.
>
> Ideas ? Or just a possible bug in VC9 which I need to duplicate and
> presenrt to Microsoft ?
>

Anyway -- just my 6¢ before you think about mailing Bill -- oh no wait --
he retired it's Steve now.

HTH

O.

0 new messages