From the course: Secure Coding in C

Unlock this course with a free trial

Join today to access over 25,600 courses taught by industry experts.

Confirming when EOF has been read

Confirming when EOF has been read - C Tutorial

From the course: Secure Coding in C

Confirming when EOF has been read

- [Instructor] The EOF constant helps confirm that an end of file has occurred and you should use it as shown in this code. Once encountered, this loop breaks and the open file is closed. The program works, so what's the big deal? The problem is that the EOF is also an errorreturn value for some functions. So to be diligent, you can run a few tests. The improvements in this code confirm the legitimacy of the EOF constant. When the EOF character is encountered, a second if test checks the file handle for the EOF condition. If the value returned is not zero, the true EF was encountered and the loop breaks otherwise else here, a check is made for file errors. The F error function obtains the error status and an error message is output here. This code is run the same as the earlier example, but the EOF condition is confirmed. I've also added error checking for the F close function down here at the end of the code. In this code, the F gets function reads from the same sample file as shown…

Contents