Wide Stream I/O and In-band Control Characters

H

heyyouguys

Hi everyone, I've recently run into an issue while doing some wchar_t
stream I/O (specifically wcout), and although I've "fixed" the
problem, I'd like to get some clarification.

In the following example:

wstring somestring(L"Pretend there's other stuff in here like a (TM)
symbol and other unicode chars.");
wcout << somestring;

Is it possible for there to be characters in there that change the
state of wcout? I ask because I was walking the filesystem, hit a
certain file, and then no matter what I put into wcout from that point
forward, nothing was printed. I "fixed" it with the following:

locale::global(locale(""));
wcin.imbue(locale());
wcout.imbue(locale());
wcerr.imbue(locale());

The reason for the scare quotes, and the problem is, I have /no/ idea
why that fixed it. Do I have to escape the strings I send to cout/
wcout? I've never heard any mention of this, so if so I'm not even
sure where to start to look for the rules. Any pointers would be very
much appreciated. I imagine the environment would be important, so
here it is:

Visual C++, Microsoft Visual Studio 2005
Windows XP Pro
cmd.exe console window

Thanks!
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
[snip]
wstring somestring(L"Pretend there's other stuff in here like a (TM)
symbol and other unicode chars.");
wcout << somestring;

Is it possible for there to be characters in there that change the
state of wcout?

If your wcout fails, it changes its state. You can test it.
if(wcout)
{

}

or explicitly use wcout.good() ...
I ask because I was walking the filesystem, hit a
certain file, and then no matter what I put into wcout from that point
forward, nothing was printed. I "fixed" it with the following:

locale::global(locale(""));

This creates a locale object that represents the user's preferences
(i.e. define in LC_ALL or LANG or whatever your system is configured
with). I presume the default locale used is "C" locale (I don't know if
it is specified somewhere).
wcin.imbue(locale());
wcout.imbue(locale());
wcerr.imbue(locale());

You imbue wcout with the user's locale.
The reason for the scare quotes, and the problem is, I have /no/ idea
why that fixed it.

I presume the default locale caused the error.
Do I have to escape the strings I send to cout/
wcout? I've never heard any mention of this, so if so I'm not even
sure where to start to look for the rules. Any pointers would be very
much appreciated.

I suppose wcout was in failed state but perhaps some character send to
the console put it into a different state (this happens on Linux when
doint a cat on a binary file).
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top