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: trouble using ifstream as a class member


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


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