CERT-FIO46-C_a
In this section:
Synopsis
Do not access a closed file.
Enabled by default
Yes
Severity/Certainty
Medium/Low

Full description
Using the value of a pointer to a FILE object after the associated file is closed is undefined behavior. Programs that close the standard streams (especially stdout but also stderr and stdin) must be careful not to use these streams in subsequent function calls, particularly those that implicitly operate on them (such as printf(), perror(), and getc()).
Coding standards
- CERT FIO46-C
Do not access a closed file
Code examples
The following code example fails the check and will give a warning:
#include <stdio.h>
int close_stdout(void) {
if (fclose(stdout) == EOF) {
return -1;
}
printf("stdout successfully closed.\n");
return 0;
}
The following code example passes the check and will not give a warning about this issue:
#include <stdio.h>
int close_stdout(void) {
if (fclose(stdout) == EOF) {
return -1;
}
fputs("stdout successfully closed.", stderr);
return 0;
}