fclose-Closing File/Deleting Files

I am using Dynamic C 8.61.

I want to delete a file, using fdelete. The function description says that “The specified file must not be open”.

So, just to be cautious, I thought I’d close the file first (even though in most cases I would expect that to be the case anyway).

So I call fclose(&FPMyFile) just before a call to fdelete.

Trouble is if I call fclose before a file has ever been set up (i.e. the pointer is NULL), the system hangs.

I thought a simple test such as if(FPMYfile != NULL)fclose(&FPMyFile); would be helpful, but I can’t find the correct syntax to do the test for NULL,

any help on the syntax, or comments on the hanging would be most appreciated


You test FPMyFile as if it is a pointer, but your usage in the fclose call looks like it is a file structure, not a pointer. If it was a pointer, you would not need the & in front of it when calling fclose. Assuming it is a static structure, you should initialize it to all zero at startup, then a simple test of FPMyFile.name for zero will tell you if the file is open and exists. If it doesn’t exist or has already been closed, then FPMyFile.name will be zero.

To zero the file structure on startup:

memset(&FPMyFile, 0, sizeof(struct File));

Then a simple test before closing:

if (FPMyFile.name) fclose(&FPMyFile);

Thanks for your help. You were right it is not a pointer. I have taken over this code, and got fooled by the “FP” in the name.
I have now used your test to check for exists and open before calling fclose.

Thanks again

best regards