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

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by oler1sSide 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.

##### Share on other sites
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);}

##### Share on other sites
Quote:
 Original post by CaspianBI'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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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...

##### Share on other sites
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.

##### Share on other sites
Given that "eof()" is the only noun before the "it", why would you think that using "it" for "the file read" would be something that the reader would be able to follow?

In any case, if you really want accurate comments about your code, then post actual code rather than vague descriptions of your code.

##### Share on other sites
Quote:
 Original post by SiCraneGiven that "eof()" is the only noun before the "it", why would you think that using "it" for "the file read" would be something that the reader would be able to follow?In any case, if you really want accurate comments about your code, then post actual code rather than vague descriptions of your code.

What, people can't read minds these days? :) I thought it was a general-question-general-answer type thing, but apparently not!

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628290
• Total Posts
2981858

• 11
• 10
• 10
• 11
• 17