why doesn't this work?

J

JustSomeGuy

unsigned short x;
ifstream cin; // opened in binary mode.

cin >> x; // Doesn't work.
yet
cin.read((char *) &x, sizeof(x)); works...
 
D

David Harmon

unsigned short x;
ifstream cin; // opened in binary mode.

cin >> x; // Doesn't work.
yet
cin.read((char *) &x, sizeof(x)); works...

Define "doesn't work".

Remember that operator>> is always used for formatted input, regardless
of stream type, and read() is raw input (possibly after text mode
manipulations.)
 
J

JustSomeGuy

David Harmon said:
Define "doesn't work".
The content of the varable is not correct.


Remember that operator>> is always used for formatted input, regardless
of stream type, and read() is raw input (possibly after text mode
manipulations.)

As I said the stream was open as binary.
 
G

Gianni Mariani

JustSomeGuy said:
As I said the stream was open as binary.

Opening the stream as binary just means that on some implementations
certain "funny" things happen to character sequences like "<CR>/<LF>".

If you want to interpret the stream differently, you'll need to provide
your own operator>>.
 
R

Ron Natalie

As I said the stream was open as binary.
That makes no difference. There are two concents.

Binary is just the opposite of text mode, which disables the implementation
specific end of line mapping (for example, \n to CR-LF mapping on some
systems).

The other concept is formatted versus unformatted I/O.
read does unformatted I/O, it just transfers a certain number of bytes
from the file to where you tell it.

Operator >> does formatted I/O. It reads the text characters from the
input stream and interprets them as a number. For example, it would
expect to see a '1' and then a '0' (and then some non numeric character)
to read a 10 from the file.
 
T

Thomas Matthews

JustSomeGuy said:
The content of the varable is not correct.






As I said the stream was open as binary.

The default mode for ifstream is text mode.
Another "feature" of formatted input is to skip whitespace,
which includes newlines, tabs, spaces, carriage-returns, formfeeds,
and vertical tabs. The read and get methods do not apply any
formmating or translations to the incoming data.

I found (and refound) this out whenever I use formatted input
to read a character. I would check for a newline and never
receive one.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Rolf Magnus

Gianni said:
Opening the stream as binary just means that on some implementations
certain "funny" things happen to character sequences like "<CR>/<LF>".

Actually, it usually means that those "funny" things do _not_ happen.
 

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
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top