For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > cin.read() failure









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author cin.read() failure
Joel Whitehouse

2005-06-02, 4:01 pm

Hello All,

I'm having some trouble with cin and ifstreams while reading some binary
data. Everytime I read in character #10 (Line feed,) the stream fails,
and I am unable to read any further from the stream, even if I use
cin.clear() or cin.p().

What can do to be able to read past a Line Feed?

-Joel
Emil Kvarnhammar

2005-06-02, 8:59 pm

Hi Joel,

Somethin' like this?

ifstream ifs("file.bin", ios::binary);
int ch;
ifs >> noskipws;

while(!ifs.eof()) {
ifs >> ch;
cout << hex << ch;
}
cout << endl;
ifs.close();
return 0;

regards
Emil Kvarnhammar
http://www.ynax.com



"Joel Whitehouse" <joelwhitehouse@gmail.com> wrote in message
news:uCY0FQ4ZFHA.1448@TK2MSFTNGP09.phx.gbl...
> Hello All,
>
> I'm having some trouble with cin and ifstreams while reading some binary
> data. Everytime I read in character #10 (Line feed,) the stream fails,
> and I am unable to read any further from the stream, even if I use
> cin.clear() or cin.p().
>
> What can do to be able to read past a Line Feed?
>
> -Joel



Igor Tandetnik

2005-06-03, 3:59 am

"Emil Kvarnhammar" <info@ynaxREMOVETHIS.com> wrote in message
news:%23DQIx27ZFHA.2520@TK2MSFTNGP09.phx.gbl
> ifstream ifs("file.bin", ios::binary);
> int ch;
>
> while(!ifs.eof()) {
> ifs >> ch;
> cout << hex << ch;
> }


Note that, even though the stream was opened in binary mode, operator>>
still performs formatted input. In particular, ifs >> ch, where ch is of
type int, tries to read digits from the stream and build a decimal
representation of a number from them - it does not read one byte at a
time as your code seems to imply. You probably meant to declare ch as
char.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Joel Whitehouse

2005-06-03, 4:02 pm

Wow! Thanks guys! That combination of suggestions worked perfectly!

Have a great day!

-Joel


Igor Tandetnik wrote:
> "Emil Kvarnhammar" <info@ynaxREMOVETHIS.com> wrote in message
> news:%23DQIx27ZFHA.2520@TK2MSFTNGP09.phx.gbl
>
>
>
> Note that, even though the stream was opened in binary mode, operator>>
> still performs formatted input. In particular, ifs >> ch, where ch is of
> type int, tries to read digits from the stream and build a decimal
> representation of a number from them - it does not read one byte at a
> time as your code seems to imply. You probably meant to declare ch as
> char.

Carl Daniel [VC++ MVP]

2005-06-03, 4:02 pm

Igor Tandetnik wrote:
> "Emil Kvarnhammar" <info@ynaxREMOVETHIS.com> wrote in message
> news:%23DQIx27ZFHA.2520@TK2MSFTNGP09.phx.gbl
>
> Note that, even though the stream was opened in binary mode,
> operator>> still performs formatted input. In particular, ifs >> ch,
> where ch is of type int, tries to read digits from the stream and
> build a decimal representation of a number from them - it does not
> read one byte at a time as your code seems to imply. You probably
> meant to declare ch as char.


Also, if you're doing binary I/O, you probably should be dealing with
std::streambuf directly instead of using a stream class. The stream
classes, as Igor pointed out, are really intended for doing formatted text
I/O, while the streambuf classes directly support raw binary reading and
writing.

-cd


Joel Whitehouse

2005-06-06, 4:02 pm

Carl Daniel [VC++ MVP] wrote:
> Igor Tandetnik wrote:
>
>
>
> Also, if you're doing binary I/O, you probably should be dealing with
> std::streambuf directly instead of using a stream class. The stream
> classes, as Igor pointed out, are really intended for doing formatted text
> I/O, while the streambuf classes directly support raw binary reading and
> writing.
>
> -cd
>
>


Thanks Carl. I'll look into that.

-Joel
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com