-2

I'm writing a program that scans a file and in doing so calls seekg() a lot. It spins in a loop, the condition of which is !infile.eof(); so it should exit the loop when it gets to the end of the file. However, for some reason it gets stuck. I suspect that the reason for this might be because it is reaching the end of the file while in seekg(). In other words, if I am 50 bytes from the end of the file and I call seekg() and tell it to move the pointer 100 bytes, maybe that makes it get stuck (either in seekg() or after that). So, what does seekg() in fact do when asked to move the pointer past the end of the file?

Thank you in advance.

user207421
  • 289,834
  • 37
  • 266
  • 440
Isaac D. Cohen
  • 773
  • 2
  • 9
  • 23
  • 1
    Are you sure your expectations and use of `eof` are correct? Are you sure the rest of your code and logic is correct? This is why including a [mcve] is required for this type of of question.. I suggest you visit the [help center](http://stackoverflow.com/help) and review the section [how do I ask a good question](http://stackoverflow.com/help/how-to-ask). – Captain Obvlious May 26 '17 at 00:01
  • 3
    You can only use `infile.eof()` after trying to read something. It tells you if the last read failed because you were at the end of the file. – Barmar May 26 '17 at 00:03
  • See http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Barmar May 26 '17 at 00:04
  • There are many variables in my code. If I included it I would have to explain what they all do. I'm just asking if seekg() could have something to do with it. – Isaac D. Cohen May 26 '17 at 00:05
  • "I would have to explain what they all do" not if they all have good, descriptive names. Seems like a drag, but saves a lot of time in the long run. – user4581301 May 26 '17 at 00:06
  • So reduce your code to a minimal example that exhibits the behavior you are encountering. – Captain Obvlious May 26 '17 at 00:08
  • 1
    Anyway the beauty of the MCVE is you don't have to provide all of your code. You have to provide a specially constructed test case that demonstrates the problem with a minimum of muss and fuss. As an added bonus, creating a MCVE almost always exposes the bug and saves you from having to make the question in the first place. – user4581301 May 26 '17 at 00:08
  • Have you considered consulting the [documentation](http://www.cplusplus.com/reference/istream/istream/seekg/)? It does *not* set the `eofbit`, so any expectation that a following `eof()` test shoudl return true is misplaced. This is all documented. – user207421 May 26 '17 at 00:33

1 Answers1

2

Nothing special happens when you seek past the end of the file. You will only notice that you've gone past the end when you next try to read from the file. This allows some other process to add to the file between the time you seek and when you read -- in that case the read will succeed.

After you seek past the end, if you try to read from the file it will fail, and set the eofbit of the stream. The next time you call infile.eof() after that, it will return true.

In general, you can't use infile.eof() until after you try to read something. This is why loops like:

while (!infile.eof()) {
    ...
}

are always wrong -- it checks for EOF before trying to read, rather than after the read has failed. See http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong

Barmar
  • 596,455
  • 48
  • 393
  • 495