This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: trouble using ifstream as a class member
- From: Jonathan Wakely <cow at compsoc dot man dot ac dot uk>
- To: Gerald Kroisandt <gerald dot kroisandt at fcc dot chalmers dot se>
- Cc: libstdc++ at gcc dot gnu dot org, withaar at wylch dot fastmail dot fm, phil at jaj dot com
- Date: Fri, 28 May 2004 10:08:38 +0100
- Subject: Re: trouble using ifstream as a class member
- References: <40B6FF25.7060507@fcc.chalmers.se>
On Fri, May 28, 2004 at 10:58:13AM +0200, Gerald Kroisandt wrote:
> The problem is that if one has a variable of type ifstream within a
> class, then the status-bits are not set to 0, if one even closes the
> file and then opens it again, i.e. if one has read the file until the
> eof-status-bit was set, then it does not help to write
> tis.close();
> tis.open(filename);
> because the eof-bit is still set although tis.tellg() gives that it is
> at the beginning (in my code, I first tried with tis.seekg(0) to come to
> the beginning and then I saw that even closing and opening does not help
> at all)!
This is not a bug, it's a required behaviour, see
http://gcc.gnu.org/onlinedocs/libstdc++/faq/index.html#4_4
> What might help (I have found this solution yesterday evening, but not
> tested totally) is:
> tis.close();
> tis.open(filename);
> tis.clear();
>
> So, at least to write somewhere the command tis.clear() which sets all
> status-bits to 0 explicitely, but one must be careful not to use it too
> often!
Since you (presumably) only want to clear the EOF bit set before close(),
you should call clear() after close() but before open(), in case open()
sets failbit or similar.
> This problem also occurs on IBM-machines, but not on a SUN!
Really? AFAIK this behaviour should be the same on all platforms.
jon
--
"It is useless to attempt to reason a man out of a thing
he was never reasoned into."
- Jonathan Swift