why doesn't this work?



 DEVELOP > c-Plus-Plus > why doesn't this work?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "JustSomeGuy"
Date: 20 Dec 2003 01:00:06 PM
Object: why doesn't this work?
unsigned short x;
ifstream cin; // opened in binary mode.
cin >> x; // Doesn't work.
yet
cin.read((char *) &x, sizeof(x)); works...
.

User: "David Harmon"

Title: Re: why doesn't this work? 20 Dec 2003 01:27:00 PM
On Sat, 20 Dec 2003 19:00:06 GMT in comp.lang.c++, "JustSomeGuy"
<nope@nottelling.com> was alleged to have written:

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.)
.
User: "JustSomeGuy"

Title: Re: why doesn't this work? 20 Dec 2003 02:27:55 PM
"David Harmon" <source@netcom.com> wrote in message
news:4054a1f4.412164894@news.west.earthlink.net...

On Sat, 20 Dec 2003 19:00:06 GMT in comp.lang.c++, "JustSomeGuy"
<nope@nottelling.com> was alleged to have written:

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".

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.
.
User: "Ron Natalie"

Title: Re: why doesn't this work? 20 Dec 2003 02:38:57 PM
"JustSomeGuy" <nope@nottelling.com> wrote in message
news:fh2Fb.760791$pl3.393454@pd7tw3no...


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

User: "Gianni Mariani"

Title: Re: why doesn't this work? 20 Dec 2003 02:34:36 PM
JustSomeGuy wrote:

"David Harmon" <source@netcom.com> wrote in message

....

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.

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>>.
.
User: "Rolf Magnus"

Title: Re: why doesn't this work? 23 Dec 2003 03:15:42 AM
Gianni Mariani wrote:

JustSomeGuy wrote:

"David Harmon" <source@netcom.com> wrote in message

...

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.


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


User: "Thomas Matthews"

Title: Re: why doesn't this work? 22 Dec 2003 08:25:49 AM
JustSomeGuy wrote:

"David Harmon" <source@netcom.com> wrote in message
news:4054a1f4.412164894@news.west.earthlink.net...

On Sat, 20 Dec 2003 19:00:06 GMT in comp.lang.c++, "JustSomeGuy"
<nope@nottelling.com> was alleged to have written:

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".


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.

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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER