Does feof() checks for eof for the current position of filepointer or checks for the position next to current filepointer?
Thanks for your help !
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Every
FILEstream has an internal flag that indicates whether the caller has tried to read past the end of the file already.feofreturns that flag. The flag does not indicate whether the current file position is as the end of the file, only whether a previous read has tried to read past the end of the file.As an example, let’s walk through what happens, when reading through a file containing two bytes.
This is why the following is the wrong way to use eof when reading through a file:
Because of the way
feofworks, the end-of-file flag is only set oncegetctries to read past the end of the file.
getcwill then returnEOF, which isnot a character, and the loop construction causes
putcharto try to write itout, resulting in an error or garbage output.
Every C standard library input method returns an indication of success or
failure:
getcreturns the special valueEOFif it tried to read past theend of the file, or if there was an error while reading. The special value is
the same for end-of-file and error, and this is where the proper way to use
feofcomes in: you can use it to distinguish between end-of-file and errorsituations.
There is another internal flag for
FILEobjects for error situations:ferror. It is often clearer to test for errors instead of “not end of file”.An idiomatic way to read through a file in C is like this:
(Some error checking has been elided from examples here, for brevity.)
The
feoffunction is fairly rarely useful.