This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC 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: libstdc++/10361: ifstream get function failure


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


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