I wish to know what happens to FILE pointer after the file is closed. Will it be NULL?
Basically, I want to check if a file has already been closed before closing a file.
For example as follows:
FILE *f;
if(f!=NULL)
{
fclose(f);
}
Can I do this or is there any other way to go about it?
Since arguments are passed by value there is not way fclose could set your file pointer to NULL. Since fclose probably destroys the FILE you have to
NULL after doing a fclose (won't work if you close it in a different function unles you use FILE **)FILE * It's a pointer to a FILE structure, when you call fclose() it will destroy/free FILE structure but will not change the value of FILE* pointer means still it has the address of that FILE structure which is now not exits.
same things happem with any pointer getting with malloc
int a malloc(10);
free(a);
still a will not be NULL
in most case i always do this things
free(a);
a=NULL;
Edit: you can not check whether its CLOSED/freed at any time. just to make sure you can assign it NULL after free/fclose so you can check its NULL or not and go ahead ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With