libstdc++/10361: ifstream get function failure
Giovanni Bajo
giovannibajo@libero.it
Wed Apr 9 18:06:00 GMT 2003
The following reply was made to PR libstdc++/10361; it has been noted by GNATS.
From: "Giovanni Bajo" <giovannibajo@libero.it>
To: <gcc-gnats@gcc.gnu.org>,
<duhovka@tau.ac.il>,
<gcc-bugs@gcc.gnu.org>,
<nobody@gcc.gnu.org>,
<gcc-prs@gcc.gnu.org>,
<paolo@gcc.gnu.org>
Cc:
Subject: Re: libstdc++/10361: ifstream get function failure
Date: Wed, 9 Apr 2003 19:57:04 +0200
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&p
r=10361
Dina Duhovny:
>It IS a BUG. If function 'get(char&)' does not read EOL if it appeares
> solely in a line, then either you should change the documentation to say
> so (and call it a "feature") or call it by its name and fix it.
That's _your_ opinion. Official C++ standard says, at §27.6.1.3:
basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb,
char_type delim );
"Extracts characters and inserts them in the output sequence controlled by
sb. Characters are
extracted and inserted until any of the following occurs:
[...]
-- c == delim for the next available input character c (in which case c is
not extracted);
[...]
If the function inserts no characters, it calls setstate(failbit), [...]".
Since a blank line only has a "\n", the function will extract no characters,
and thus will set failbit. You don't clear the failbit, so the function
fails next time immediatly; you keep waiting for eof, which will never occur
since no more characters will be read.
To avoid the infinite loop, you should check simply for !pdb instead of
!pdb.eof().
Besides, there is a better way to do what you're trying to do: just use
ifstream::getline(). First, getline() silently skips "\n", so you don't have
to explicitally call get() to ignore the "\n". Second, it sets the failbit
if no characters are _EXTRACTED_ from the stream, instead of _STORED_ like
get(). So, in case of a blank line, the "\n" is extracted and silently
skipped, the function returns and failbit is not set, so everything
continues as usual.
> Remember, it worked (as it should) before 3.0.
It means that your code was relying on a bug present in libstdc++ before 3.0
It does not mean that your code should work.
Giovanni Bajo
More information about the Gcc-prs
mailing list