man/checkers/fcloseInLoopCondition.md
Message: fclose() used as loop condition may skip loop body or double-close file handle.
Category: Resource Management
Severity: Warning
Language: C and C++
Using fclose() as a loop condition leads to two unwanted outcomes:
fclose() is called again on the already-closed file handle on the next iteration, which is undefined behaviour.This pattern is almost always a misunderstanding of what fclose() returns, or confusion with a function that reads/processes data incrementally (like fgets or fread). Unlike those functions, fclose() is a one-shot teardown operation and has no meaningful retry or loop-until-done semantic.
Call fclose() outside the loop condition. If you need to check whether the close succeeded, store the return value and test it separately.
Before:
FILE *fp = fopen("data.txt", "r");
while (fclose(fp)) {
/* process file */
}
After:
FILE *fp = fopen("data.txt", "r");
/* process file */
if (fclose(fp) != 0) {
/* handle close error */
}
useClosedFile - for using a file handle that has already been closed