1

I'm pretty new to programming, and recently, my class started file handling using C++. My question is what triggers an end of file? Just like there is "\n" for a new line character, is there something specific that the compiler looks at to know if it is eof? An answer with lots of juicy details is much appreciated!

5
  • 1
    "What triggers and End of File?" - that there is no more data in the file. That you are exceeding the number of bytes in the file. What else would it be? Commented Jul 27, 2019 at 11:21
  • 1
    The eof condition flag on iostreams is triggered by attempting to read past the end of the file Commented Jul 27, 2019 at 11:23
  • 2
    End of file is triggered when reading past the last character that is present in an input stream. Different systems recognise that occurrence in different ways but, however it is recognised, the standard library functions convert that into an appropriate states (e.g. set a flag in the stream state). Commented Jul 27, 2019 at 11:36
  • 3
    This may help you avoid a bug in the future: stackoverflow.com/questions/5605125/… Commented Jul 27, 2019 at 11:47
  • 2
    latedev.wordpress.com/2012/12/04/all-about-eof has a good overview of EOF. Commented Jul 27, 2019 at 12:41

1 Answer 1

3

End Of File (EOF) isn't tied to a specific character like End Of Line (EOL / LF) is. It's simply a state to signal "we're at the end of this file", different methods will have different way of signalling this. Usually this EOF "state" is triggered when the code tries to read past the end of the file.

For example, when using an ifstream, the get() method will return Traits::eof() if we've read all the data in the file. After that point, calling std::ifstream::eof() will return true. This only applies to C++'s streams though, different methods and/or languages may have different ways of signalling EOF, it's not a universal thing like EOL is, there's no character to describe EOF.

Sign up to request clarification or add additional context in comments.

2 Comments

Very, very strong emphasis on the "tries to read past the end of file" part, rather than just at the end. I wasted a lot of time and stupid band-aid code when I first started with C++ on not knowing that distinction.
@SombreroChicken This is exactly what I was trying to find! Before reading this, I was guessing that there must be some specific character that would signal an eof. What I take from all the responses above is that an eof is simply defined as a point after which no further bytes can be read (because the file has ended, obviously). Various methods are there to find this out. Cool. It feels good now that I understand this!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.