This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: bug in istream.get(b,c,d) ?
- From: Loren James Rittle <rittle at latour dot rsch dot comm dot mot dot com>
- To: libstdc++ at gcc dot gnu dot org
- Cc: mark dot reed at turner dot com
- Date: Wed, 18 Sep 2002 17:30:42 -0500 (CDT)
- Subject: Re: bug in istream.get(b,c,d) ?
- Organization: Networks and Infrastructure Lab (IL02/2240), Motorola Labs
> If a get(buffer, count, delim) call reads 0 characters
> (because the next character on the stream is a delimiter),
> then a subsequent call to get(void) returns EOF even though
> there are more characters on the stream to be read. I
> don't have the C++ specification, so I'm not sure if this
> is counter to spec, but it is definitely surprising behavior.
Mark, please consider obtaining an e-copy of the C++ specification for
$15 from ANSI. ;-)
To address your exact question:
Section 27.6.1.3 clause 3, 4:
int_type get();
Effects: Extracts a character c, if one is available. [...]
Returns: c if available, otherwise traits::eof().
Thus, EOF could be returned if a character is not available for any
reason. Attempting to read more input from a failed stream could
produce this result.
Section 27.6.1.3 clause 7 - 9:
[...] [get (buffer, count, delim)] [...]
If the function stores no characters, it calls setstate(failbit) [...]
Do you now see what happened? If you want, you may call clear() to
"correct" the situation. Alternately, I might suggest that for a
simple C++ version of cat(1), you picked mighty low-level APIs at
which to write your code. Consider (line at a time version of cat
which alway adds a final newline if not present):
#include <iostream>
#include <fstream>
#include <string>
int
main (int argc, char *argv[])
{
std::string s;
for (int i = 1; i < argc; i++)
{
std::ifstream file(argv[i], std::ios::in);
while (getline (file, s))
std::cout << s << std::endl;
}
}
Regards,
Loren