Why is this an issue?
A call to the fopen()
/open()
function must be matched with a call to fclose()
/close
. Otherwise,
you run the risk of using up all the OS’s file handles, which could lock up not just your program but potentially everything on the box.
Noncompliant code example
int fun() {
FILE *f = fopen("file", "r");
if (f == NULL) {
return -1;
}
// ...
return 0; // Noncompliant, file f has not been closed
}
Compliant solution
int fun() {
FILE *f = fopen("file", "r");
if (f == NULL) {
return -1;
}
// ...
fclose(f);
return 0;
}
Resources