From the course: Secure Coding in C

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Confirming when EOF has been read

Confirming when EOF has been read - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Confirming when EOF has been read

- [Instructor] The EOF constant is frequently used to confirm the end-of-file as has been encountered. In this code, the EOF constant is tested at line 22, as individual characters are read from a file. Once encountered, the loop breaks and the open file is closed, and it works. The improvements in this code further verify the legitimacy of the EOF constant, which can also be generated by some functions as an error condition. The EOF function confirms the end-of-file at line 25. If not, the ferror function obtains the error status with a message output at line 38. This code runs the same as the earlier example that the EOF condition is confirmed. And I also added error checking for the fclose statement here at line 48, which also returns the EOF in case it errors. In this code, the fgets function reads from the sample file, but one line at a time. The feof functon is used at line 24 to confirm that the EOF has actually…

Contents