This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: bug in istream.get(b,c,d) ?


> 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]