When I open a file in C, I am currently doing this:
int main()
{
FILE *f
f = fopen(\"employers.dat\", \"rb\");
if(f == NULL)
{
PUTS(\"can
Not only it is not necessary to use fclose()
when f
is NULL
, but you should actually not invoke fclose()
when f
is NULL
.
If f
is NULL
, then the file was never opened to begin with, so it does not need any closing.
Even if the file somehow needed closing, the solution could not possibly involve passing NULL
to fclose()
, because a NULL
parameter carries absolutely no information that fclose()
can use to figure out which file to close.
I am not sure whether fclose()
contains extra code for detecting and ignoring an erroneous NULL
parameter passed to it, but even if it does, it is best to not tempt your fate.
Since the file is not opened there is no need to close the file.