Re: wstring, wofstream, and encodings
On 12 avr, 09:51, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.com> wrote:
"Jeffrey Walton" <noloa...@gmail.com> wrote in message
news:5fb40192-0a98-40e2-971e-35754e9ca764@b64g2000hsa.googlegroups.com...
I'm attempting to write a wstring to a file by way of wofstream. I'm
getting compression on the stream (I presume it is UTF-8). How/where
do I invoke an alternate constructotor so that the stream stays wide
(UTF-16)?
I suspect that it is hidden in a locale, but I don't have much
experience with them. I also have not been able to locate it in
Stroustrup: Appendix D: Locales [1]. [1] does state the following, but
I do not have section 21.7: "Section =A721.7 describes how to change
locale for a stream; this appendix describes how a locale is
constructed out of facets and explains the mechanisms through which a
locale affects its stream."
From my understanding of iostream, locales will not be the answer.
Locales apply to the upper layer of the iostream, which takes
care of converting values to characters. They affect the
choice of the characters used to represent a value, but not
the encoding of these characters.
That would be logical, but... std::streambuf maintains a locale
too, which can be imbued (and imbuing an istream or an ostream
imbues the attached streambuf, if any). Most streambuf ignore
the locale, but filebuf uses it for code translation.
The internal filebuf or basic_filebuf is the object that will
determine how the in-memory characters are wirtten to a file.
This is the layer (the stream *buffer*) that can define whether
a file is written using UTF8 or another character encoding.
However, the C++ standard does not specify an interface
allowing to select what character encoding is to be used by
(w)filebuf.
wfilebuf::imbue().
Note that you can call it independantly of the iostream, e.g.:
std::ifstream input( ... ) ;
input.imbue( someLocale) ;
input.rdbuf()->imbue( someOtherLocale ) ;
On the other hand, imbuing the iostream will imbue the attached
streambuf, even if this streambuf is also used by other
iostreams.
With regards to the posters original question:
std::wofstream output( ... ) ;
output.imbue( desiredLocale ) ;
should do the trick. Provided he can find the desired locale.
Your best bet would be to ask your question on a platform-
specific forum, related to the library implementation you use.
He'll probably have to go that route in order to find what
locales are available, and how to name them.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34