Why is this an issue?
Using the value of a pointer to a FILE
object after the associated file is closed is undefined behavior.
Noncompliant code example
void fun() {
FILE * pFile;
pFile = fopen(fileName, "w");
if (condition) {
fclose(pFile);
// ...
}
fclose(pFile); // Noncompliant, the file has already been closed
}
Compliant solution
void fun() {
FILE * pFile;
pFile = fopen(fileName, "w");
if (condition) {
// ...
}
fclose(pFile);
}
Resources