iostream question: how to change cout to binary?

  • Thread starter Thread starter Charles F McDevitt
  • Start date Start date
C

Charles F McDevitt

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?
 
Charles said:
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?

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 said:
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.

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.
 
Charles F McDevitt said:

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

--
(e-mail address removed) 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
 
Standard streambufs don't have files associated with them.
That's how the old iostreams worked.

Hendrik Schober said:
Charles F McDevitt said:

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

--
(e-mail address removed) 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 said:
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.

Schobi

--
(e-mail address removed) 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
 
Back
Top