[c++] What's a better end-of-file break than eof()?

Started by
10 comments, last by Side Winder 15 years, 11 months ago
eof() doesnt seem to work very well. It'll get to the end of the file, but then try and read in another line (one that's not even there). Is there a better function I can call for this?
Advertisement
eof() doesn't perform any reads.
Side Winder, what exactly do you think eof() does? What do you imagine its purpose to be? Hint: You can't tell whether the next read will fail on EOF...until you actually try to read it.
Quote:Original post by oler1s
Side Winder, what exactly do you think eof() does? What do you imagine its purpose to be? Hint: You can't tell whether the next read will fail on EOF...until you actually try to read it.


Is that not what I said? It reads in another line. I'm trying to find an alternative that DOES perform an end-of-file correctly.
I'm not sure if you misunderstand fstream::eof() or are just looking for a different solution. But fstream::eof() works perfectly. It returns true or false based on whether the last read failed and that's exactly what it is supposed to do.

If you want to see IF the next read will fail, then you can try this.

ifstream inFile("someFile.txt");while ( inFile ){   if ( inFile.peek() == EOF ) break;   string line;   getline(inFile, line);}
Quote:Original post by CaspianB
I'm not sure if you misunderstand fstream::eof() or are just looking for a different solution. But fstream::eof() works perfectly. It returns true or false based on whether the last read failed and that's exactly what it is supposed to do.

If you want to see IF the next read will fail, then you can try this.

*** Source Snippet Removed ***


Compare:
std::string line;while(std::getline(inFile,line)){   // ...}


This also handles non end of file errors nicely.

Quote:Is that not what I said? It reads in another line.
So, it follows that if I wanted to read an entire file, the only thing I would have to do is repeatedly call eof(), because it just keeps reading lines. No, it doesn't work like that. Where did you get the idea that eof reads a line? I assumed you looked up eof in some documentation. So I'm curious to know what C++ reference or documentation suggested that eof does any kind of reading.

As SiCrane succintly said, eof does not read. eof() returns a bool, that is true if eofbit is set. As I hinted earlier, the only way you can tell if you will hit an EOF is to actually attempt a read, and then check if it fails. When a read fails, you actually don't know why it failed. That's where eof comes in handy. It tells if you the failure was because of EOF.

Which brings me to: there is no prescience involved here. You cannot know whether your next read will succeed, until you actually attempt to read. So you should check your read to see if it actually worked. Do you do that? When you call fgets, fscanf, or whatever, do you check if it works?

In fact, this is how rip-off's work sort of works. It attempts to read a line repeatedly, only executing the code in the while loop if the read succeeds.

Quote:I'm trying to find an alternative that DOES perform an end-of-file correctly.
Oh, and, if your code doesn't work, it's a good bet you wrote something wrong.
I'm aware that eof() doesn't read in lines. I don't think I ever said I did? Only that I was using it as an end-of-file check... Either way, I've now put in some checks into my class to check if a value is null, or 0, and if it is, not to declare it.
Quote:I'm aware that eof() doesn't read in lines. I don't think I ever said I did?
Quote:eof() doesnt seem to work very well. It'll get to the end of the file, but then try and read in another line
Quote:Is that not what I said? It reads in another line.
Uh...

Quote:Either way, I've now put in some checks into my class to check if a value is null, or 0, and if it is, not to declare it.
I'm not entirely sure how to interpret this statement. Something sounds wrong here, but hey, it sounds like your problem is fixed...
Quote:eof() doesnt seem to work very well. It'll get to the end of the file, but then try and read in another line


By "it" I meant the file read, not eof(); as in, I know it's got to the end of the file, but the eof() method didn't realise it had, and there would be another line read in.

This topic is closed to new replies.

Advertisement