Advertisement

Detecting End of File using ifstream

Started by July 14, 2004 04:50 PM
3 comments, last by GameDev.net 20 years, 4 months ago
I'm trying to write an OpenGL program that reads text in from a file and displays it onscreen. So far I've not had problems with my text buffer, but I'm having difficulty detecting the end of the file. My ifstream is named input, and when I test for input.eof(), it never returns false. I've also tried (input.rdstate() & ifstream::failbit ) != 0 to detect if there was an error reading the input, but so far no luck whatsoever. Any ideas?
How are you reading from the ifstream?
Advertisement
.eof(), as in C, will only return false after a failed operation. For this reason, what you would normally do is attempt a read as normal, and if that failed, break out of your loop. Generally you only check for .eof() to make sure that you reached the end of the file and didn't actually encounter an error.
alternatively you can define hom many Polys ur loading
// Map Edit V2.1
NumFaces = 21
NumVerts = 200
//Face 1
.
.
.
//Face 2
.... After 21 Faces
// Vert 1
.
.
.
----------------------------

http://djoubert.co.uk
how about using tellg() and seekg() to get size?

cur_pos = tell_g();seekg(ios_base::end);size = tell_g();seekg(ios_base::beg, cur_pos); then just compare tellg() to size while you read.. slow?

This topic is closed to new replies.

Advertisement