empriser said:
FILE *fp;
fp = fopen( ... )
fclose( fp ).
How do I know if fp has been fclose.
You have to remember that you've closed it. (Or, preferably,
arrange that you don't have to care.)
I know can set fp to NULL after fclose it, then check fp's value to
solve the problem.
That doesn't help if there are other copies of `fp`s value.
A useful trick is to arrange that your file-handling code looks like:
{ FILE* fp = fopenSuitably()
; doThingsWithFile( fp )
; fclose( fp )
; }
(Don't mind my layout, I'm being experimental today.)
(Insert check on `fclose` working to taste.)
Now, this piece of code is so short it's easy to check that
you never use `fp` after its been closed. (Assuming that
`doThingsWithFile` doesn't wilfully copy `fp` elsewhere.)
And sometimes you can arrange that `doThingsWithFile` is a
function pointer. (If it needs extra state, pass that in too.)
If most of your file-handling can be done that way, you only need
to be deeply suspicious of other uses of files.
Another trick is to have a struct
struct fileAndBoolean
{ YourBooleanType closed
; FILE* fp
; }
Write file-accessing methods on `struct fileAndBoolean*`. Have them
check whether `closed` is true. Set `closed` on `close()`.
Whether these tricks are of value to you depends on your context.