std::wofstream

K

kaalus

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

Result: file 'a' is created and it contains 1 byte. I expected it
should contain 2 bytes...? So I tried what happens when character is
outside 0-255 range:

std::wofstream s("a");
s << L'.'; // in place of . there is unicode character U+2666

Result: Writing failed and stream bad() flag is set. What's wrong here,
isn't wofstream supposed to write wide characters to stream?

cheers,
Marcin

btw. I can write wchar_t values using regular ofstream, and I get 2
bytes per character as expected. But wofstream doesn't seem to work.
 
A

Alf P. Steinbach

* (e-mail address removed):
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();

Result: file 'a' is created and it contains 1 byte.

Right. It converts, to whatever. In your case probably Latin-1 or UTF-8.
 
M

Marcin Kalicinski

std::wofstream s("a");
Right. It converts, to whatever. In your case probably Latin-1 or UTF-8.

Ok, but why I cannot output any char outside 0-255 range? Stream state is
set to bad then.

Also, although I know it is an off-topic question, do you know where can I
find docs on what encoding method is used by implementation of wofstream in
MSVC .NET 2003? I dug deep in MSDN but found absolutely no clue...

Marcin
 
J

John Harrison

Marcin said:
Ok, but why I cannot output any char outside 0-255 range? Stream state is
set to bad then.

Also, although I know it is an off-topic question, do you know where can I
find docs on what encoding method is used by implementation of wofstream in
MSVC .NET 2003? I dug deep in MSDN but found absolutely no clue...

I suggest firing up your debugger and stepping in the code for

s << L'A';

then you will find out what is really going on. It might get quite hairy
however.

john
 
M

Marcin Kalicinski

Also, although I know it is an off-topic question, do you know where can
I suggest firing up your debugger and stepping in the code for

s << L'A';

then you will find out what is really going on. It might get quite hairy
however.

I did just that, and debugging ends on call to sbumpc() for which I do not
have sources.

Marcin
 
J

John Harrison

Marcin said:
I did just that, and debugging ends on call to sbumpc() for which I do not
have sources.

Marcin

You're using MSVC? You should have the sources, need to go back to the
install disk and make sure you pick the option that gives you the
sources. Or maybe you need to set the compiler/debugger options that
tells it where to find the sources.

Try searching on your hard disk for the string "sbumpc" I expect it will
be there.

john
 
P

P.J. Plauger

You're using MSVC? You should have the sources, need to go back to the
install disk and make sure you pick the option that gives you the sources.
Or maybe you need to set the compiler/debugger options that tells it where
to find the sources.

Try searching on your hard disk for the string "sbumpc" I expect it will
be there.

Try searching include/streambuf.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
P

P.J. Plauger

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

Result: file 'a' is created and it contains 1 byte. I expected it
should contain 2 bytes...? So I tried what happens when character is
outside 0-255 range:

std::wofstream s("a");
s << L'.'; // in place of . there is unicode character U+2666

Result: Writing failed and stream bad() flag is set. What's wrong here,
isn't wofstream supposed to write wide characters to stream?

The default conversion under Windows is 1-to-1. If you want a
fancier rule you need a different codecvt facet. See our CoreX
library for a large assortment.
btw. I can write wchar_t values using regular ofstream, and I get 2
bytes per character as expected. But wofstream doesn't seem to work.

Both "work" but by different rules.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top