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

iostream question: how to change cout to binary?

13 views
Skip to first unread message

Charles F McDevitt

unread,
Oct 4, 2003, 10:58:19 AM10/4/03
to
I'm converting some old programs that use old iostreams.

In one program, the program is using cout to output to the stdout stream.
Part way through, the program wants to put some binary data out, and changes
the iostream to binary like this:
cout << "this is text" << eol;
binary(cout);
cout << "this is binary" << eol;
text(cout);
cout << "back to text mode" << eol;

Of course, with standard iostreams there aren't the binary() or text()
functions.

How can I make this switch? I can't close and re-open the iostream, since
I have no idea what stdout is connected to. And it appears the only way to
change the mode is in open().

Am I missing something? Is this functionality just not available?
Do I need to rewrite the program in C ?

Is there anything that can be done with ibue() and a custom locale?


Doug Harrison [MVP]

unread,
Oct 4, 2003, 3:45:56 PM10/4/03
to
Charles F McDevitt wrote:

The CR/LF translation and Ctrl+Z detection occurs at a much lower level, and
I'm not aware of anything at the iostreams level that can affect it beyond
the open-mode. However, I believe you can use _setmode(fileno(stdout))
before you do any output through stdout or cout. But you want to switch back
and forth between text and binary modes. I don't know if that will work with
_setmode. I wouldn't expect it to work unless you also flushed the buffer
before each mode change.

--
Doug Harrison
Microsoft MVP - Visual C++

Charles F McDevitt

unread,
Oct 5, 2003, 9:47:03 AM10/5/03
to

"Doug Harrison [MVP]" <d...@mvps.org> wrote in message
news:607unv8rfb28blmn6...@4ax.com...

Thanks... I already decided to try _setmode, but I really don't know if it
will work
for switching back and forth. I'll cross my fingers and hope.


Hendrik Schober

unread,
Oct 7, 2003, 1:18:12 PM10/7/03
to
"Charles F McDevitt" <Chuck_M...@m-s-n.com> wrote:
> [...]

You can change the stream buffer 'cout' uses
at run-time. Maybe you can switch between a
buffer that's opened in binary mode and the
common one?
Oh, can you have two buffers to stdout, at
all? I don't know...

Schobi

--
Spam...@gmx.de is never read
I'm Schobi at suespammers org

"And why should I know better by now/When I'm old enough not to?"
Beth Orton


Charles F McDevitt

unread,
Oct 8, 2003, 10:08:31 PM10/8/03
to
Standard streambufs don't have files associated with them.
That's how the old iostreams worked.

"Hendrik Schober" <Spam...@gmx.de> wrote in message
news:%23ZWOldP...@TK2MSFTNGP12.phx.gbl...

Hendrik Schober

unread,
Oct 9, 2003, 6:28:18 AM10/9/03
to
"Charles F McDevitt" <Chuck_M...@m-s-n.com> wrote:
> Standard streambufs don't have files associated with them.
> That's how the old iostreams worked.
> [...]

C's 'stdout' is a file and can be opened
as such. I don't know how 'std::cout' opens
its stream. However, opening it as file
stream should be std conforming.

0 new messages