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.peek().

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

-Joel

Re: cin.read() failure by Emil

Emil
Thu Jun 02 16:51:33 CDT 2005

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.peek().
>
> What can do to be able to read past a Line Feed?
>
> -Joel



Re: cin.read() failure by Igor

Igor
Thu Jun 02 20:56:15 CDT 2005

"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



Re: cin.read() failure by Joel

Joel
Fri Jun 03 10:42:59 CDT 2005

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

Re: cin.read() failure by Carl

Carl
Fri Jun 03 12:05:07 CDT 2005

Igor Tandetnik wrote:
> "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.

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



Re: cin.read() failure by Joel

Joel
Mon Jun 06 09:12:34 CDT 2005

Carl Daniel [VC++ MVP] wrote:
> Igor Tandetnik wrote:
>
>>"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.
>
>
> 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