stream and formatting char type

B

blix

When I output my char to a stream and it has a value of 11, it writes a ^B
(vertical tab). This is understandable. When I attempt to stream it back
into a char variable, it skips it because a tab is considered whitespace.

If I cast the char to int, it'll stream out an 11 so that I can read it in
plain english. Doing this same cast while streaming the value in doesn't
read the value. I have to use a temporary int variable and then assign my
char to it.

Internally I suspect the IOStream library is using the "%c" format
specification if the type is a char. Is there a way to tell the IOStream
library to use the "%d" format specification for the char type?
 
J

Jeff Schwab

blix said:
When I output my char to a stream and it has a value of 11, it writes a ^B
(vertical tab). This is understandable. When I attempt to stream it back
into a char variable, it skips it because a tab is considered whitespace.

Use the "get" method instead of the extraction operator.
If I cast the char to int, it'll stream out an 11 so that I can read it in
plain english. Doing this same cast while streaming the value in doesn't
read the value. I have to use a temporary int variable and then assign my
char to it.

I think you've got the assignment backwards. Assign the int to the
char, not the char to the int.
Internally I suspect the IOStream library is using the "%c" format
specification if the type is a char. Is there a way to tell the IOStream
library to use the "%d" format specification for the char type?

You could overload >> in the local namespace, but may I suggest you not
bother?
 

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

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top