jacob navia <> writes:
> Nick a écrit :
[...]
>> Suppose you've opened a file with fp as the pointer and then closed it.
>> What does it check about fp?
>>
>> If there was a safe and easy way to do this, fclose could do it to check
>> you don't close the file twice (source of probably my all-time hardest
>> to spot bug: if you took a particular (and rare) path through the
>> program a file was closed twice. A year or more later I added code that
>> caused, in even rarer circumstances, another file to be opened further
>> through the flow. /That/ fopen was where it crashed).
>
>
> Fine, but an fclose that doesn't check its parameters is really a
> pile of ****... It is not that difficult to maintain a list of open
> files and verify that the file is really open before closing it.
I agree that fclose() can do some checking, and that it shouldn't be
too difficult in most implementations. (I won't comment on your
characterization of an implementation that doesn't do this.)
On the other hand, it's not hard to imagine an implementation that's
built on top of some lower level, where that lower level doesn't
provide this kind of checking.
Also (somebody else mentioned this scenario), consider:
fp1 = fopen(...);
fclose(fp1);
...
fp2 = fopen(...);
/* Suppose fp2 happens to point to the same FILE object that
fp1 had pointed to.
*/
...
fclose(fp1);
/* Oops, we closed fp1 twice, but the error isn't detected
because the pointer looks valid.
*/
...
fclose(fp2);
/* This should have beeen ok, but the previous fclose clobbered
the FILE object; if fclose() performs checking, it will
report the error here rather than where it really occurred
*/
That's not to say that performing checking in fclose() isn't a good
idea (and closing a file isn't likely to be performance-critical), but
it can't catch everything.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"