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 - 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
-
-
-
-
(Locked)
Formatting preprocessor directives1m 33s
-
(Locked)
Using an assignment as a condition1m 44s
-
(Locked)
Avoiding putchar() in a while loop2m 38s
-
(Locked)
Using the system() call2m 4s
-
(Locked)
Accessing elements beyond the array size2m 38s
-
(Locked)
Converting integers2m 57s
-
(Locked)
Looping with floating point values3m 10s
-
(Locked)
Using return values1m 52s
-
(Locked)
Confirming when EOF has been read3m 34s
-
(Locked)
Challenge: Fix the code1m 20s
-
(Locked)
Solution: Fix the code1m 54s
-
(Locked)
-
-
-
-