473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

wstring, wofstream, and encodings

Hi All,

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 §21.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."

== Sample ==
wstring ws = L"wide";

wofstream ofs;
ofs.open("wide.dat", std::ios::binary | std::ios::trunc );
if( !ofs.good() ) { return; }

ofs << ws;
ofs.close();
== End Sample ==

Thanks,
Jeff
Jeffrey Walton

[1] http://www.research.att.com/~bs/3rd_loc0.html
Jun 27 '08 #1
6 4182
"Jeffrey Walton" <no******@gmail.comwrote in message
news:5f**********************************@b64g2000 hsa.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 §21.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.

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.

Your best bet would be to ask your question on a platform-
specific forum, related to the library implementation you use.
A specific wfilebuf (/basic_filebuf) implementation may
allow you to specify the file's enocding style.
Or maybe this is configurable at an OS or C library level.
Worst case, you will still be able to write your own streambuf
layer to write files using the specific encoding you want.
I hope this helps...
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Jun 27 '08 #2
Jeffrey Walton wrote, On 12.4.2008 5:27:
Hi All,

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 §21.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."
I am too lazy to look into the standard right now but IIRC it says something
about it being implementation defined. In either case, I think you can imbue
the stream with your own locale that has custom codecvt facet.

--
VH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIAG+UoUFWwtEPkHIRCORBAJ4zQNd73YPhyP8gtG+n4A 5JPlh5owCcDiR7
lSSGMtU6qa3Ndmy3YB9ftks=
=B9oY
-----END PGP SIGNATURE-----

Jun 27 '08 #3
Hi Ivan,

On Apr 12, 3:51*am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
"Jeffrey Walton" <noloa...@gmail.comwrote in message

news:5f**********************************@b64g2000 hsa.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 §21.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.

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.

Your best bet would be to ask your question on a platform-
specific forum, related to the library implementation you use.
A specific wfilebuf (/basic_filebuf) implementation may
allow you to specify the file's enocding style.
Or maybe this is configurable at an OS or C library level.
Worst case, you will still be able to write your own streambuf
layer to write files using the specific encoding you want.

I hope this helps...
Ivan
--http://ivan.vecerina.com/contact/?subject=NG_POST<- email contact form
Brainbench MVP for C++ <>http://www.brainbench.com
Your best bet would be to ask your question on a platform-
specific forum, related to the library implementation you use.
You are correct. I asked over on microsoft.public.vc.language.
Microsoft's implementation is broken [1]. I'm kind of suprised - I
though Plaugher (sp?) supplied it through Visual Studio.

Jeff
Jeffrey Walton

[1] http://groups.google.com/group/micro...8eb7489a42b568

Jun 27 '08 #4
On Apr 12, 4:15*am, Vaclav Haisman <v.hais...@sh.cvut.czwrote:
Jeffrey Walton wrote, On 12.4.2008 5:27:Hi All,
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 §21.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."

I am too lazy to look into the standard right now but IIRC it says something
about it being implementation defined. In either case, I think you can imbue
the stream with your own locale that has custom codecvt facet.

--
VH

*signature.asc
1KDownload
Thanks VH. This will help.

Jeff
Jeffrey Walton
Jun 27 '08 #5
Hi VH,

On Apr 12, 4:15*am, Vaclav Haisman <v.hais...@sh.cvut.czwrote:
Jeffrey Walton wrote, On 12.4.2008 5:27:Hi All,
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 §21.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."

I am too lazy to look into the standard right now but IIRC it says something
about it being implementation defined. In either case, I think you can imbue
the stream with your own locale that has custom codecvt facet.
I was not able to grab a copy off the ISO web site $$$ (I wanted to
thumb through it). If you could post a link to your source, it would
be appreciated.

Jeff
Jeffrey Walton


Jun 27 '08 #6
On 12 avr, 14:42, Jeffrey Walton <noloa...@gmail.comwrote:
On Apr 12, 3:51 am, "Ivan Vecerina"
[...]
You are correct. I asked over on microsoft.public.vc.language.
Microsoft's implementation is broken [1]. I'm kind of suprised - I
though Plaugher (sp?) supplied it through Visual Studio.
Broken, or simply that the locales which do what you want aren't
delivered with VC++? (In general, Dinkumware has been well in
advance of other implementations in its implementation of
locales, and locale support in streams. And while one can never
totally discount the possibility of an error, I'd tend to
suspect rather the absense of the necessary locales.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Saurabh Aggrawal | last post by:
Hi, On line nos. 24, 25, 26 24: wstring cfMethods = {{L"setLabel"},{L""}}; 25: wstring cfProperties= {{L"isVisible"},{L""}}; 26: wstring cfEvents =...
6
by: DFB_NZ | last post by:
Hi. I have had problems compiling a simple program that uses wstring. So....I wrote a small application that reads using char, fgetc etc in a text file and writes it out to another text file....
7
by: kaalus | last post by:
Hi All, I just tried to use wide streams with C++. But something really strange is happening (my compiler is MSVC, wchar_t size is 2 bytes): std::wofstream s("a"); s << L'A'; s.close(); ...
11
by: FNX | last post by:
I'm stumbling around a little bit here trying to get a feel for wide/Unicode functionality. I'm pretty up on C++ in general, streaming stuff not so much. If I run the following code, I get a...
3
by: uday.sen | last post by:
Hi, I am porting a piece of code from VC++ to linux platform. My compiler is g++ 3.2.2. I am getting following error: no matching function for call to A::setResponse(std::wstring) candidates...
8
by: Divick | last post by:
Hi all, can somebody tell how much std::wstring is supported across different compilers on different platforms? AFAIK std::string is supported by almost all C++ compilers and almost all platforms,...
10
by: v4vijayakumar | last post by:
1. why the following program is not working as expected? #include <iostream> using namespace std; int main() { string t("test"); wcout << (wchar_t *) t.c_str() << endl; wcout << t.c_str()...
3
by: jraul | last post by:
Why does the following code create an empty file? I am using Windows XP. #include <iostream> #include <fstream> int main() { std::wofstream fout("data.txt");
1
by: Meal | last post by:
I put a chinese character into the XML file, and use const XMLCh* xvalue = personNoteNode->getFirstChild()->getNodeValue() to get the string. The begining of the string is English, and in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.