SpreadTooThin wrote:
I am simply trying to see if I am at the end of a file...
In my class I maintain a filebuf.
In my class I want to provide a method to see if i am at the eof...
You can't. The C++ IO model does not support predictive EOF.
What you can do is try to read.
My question is can I reconstruct a istream using a filebuf and ask the
istream if it is at eof?
ie...
class myclass
{
filebuf fb;
...
bool eof()
{
istream is(&fb);
return (is.eof());
}
};
Seems like a lot of work to reconstruct the istream on each
call to eof, but then how would I include the istream as a
class variable?
Not only a lot of work, but it won't work. Even if fb is at the
end of the file, is.eof() may (and probably will) return false
in the above scenario.
What's wrong with just peeking at the next character, e.g.:
bool eof()
{
return fb.sgetc() == EOF ;
}
It's not reliably repetative, but it will generally work for
real files (not keyboard, etc.), and it is reliable with the
better implementations (both Dinkumware and g++, I think,
although since I also have to contend with some poorer
implementations, it's not something I count on---but maybe I'm
out of date, and this is no longer a problem).
A more reliable solution would be to maintain a flag in your
class, like istream does. Something like:
bool eof()
{
if ( ! isEof ) {
isEof = fb.sgetc() == EOF ;
}
return isEof ;
}
Note that any code actually extracting characters from the
filebuf should alsoset isEof anytime it gets an EOF from sgetc,
sbumpc or snextc.
--
James Kanze (Gabi Software) email:
ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34