davetoulouse
Programmer
Hi All
Sorry this question is paranoid - but i am!
Since this routine will be deep in the code I want it bomb proof ( even Me proof )
As part of a bigger project I have written a trivial
routine to close a file. There is other stuff in it but I reduced it to minimum to verify where problem is.
so:
This works perfectly well except if I deliberately mess it up.
If I call CloseFile twice
The first call will work and close the file.
The second call (where the file is already closed)causes
glibc to be very unhappy with me.Program aborts and fires out a little dump.
I know my program should not call this twice
I have programmed it so it will not
BUT
my paranoia says "assume I mess up" ( I am good at messing up ) so trap the error before it happens.
I have looked everywhere i can think of and cannot find a way to do so ( or am missing it )
Can I test to see if "fp" is actualy alive so I can avoid trying to close it if it is not.
if so how?
not sure if important but:
using
gcc verion 4.4.3
make version 3.81
Many Thanks in advance
Dave
Sorry this question is paranoid - but i am!
Since this routine will be deep in the code I want it bomb proof ( even Me proof )
As part of a bigger project I have written a trivial
routine to close a file. There is other stuff in it but I reduced it to minimum to verify where problem is.
so:
Code:
#include<stdio.h>
#include<stdlib.h>
int CloseFile(FILE *fp)
{
if(fclose(fp)!=0)
{
return(-1);
}
return(0);
}
This works perfectly well except if I deliberately mess it up.
If I call CloseFile twice
The first call will work and close the file.
The second call (where the file is already closed)causes
glibc to be very unhappy with me.Program aborts and fires out a little dump.
I know my program should not call this twice
I have programmed it so it will not
BUT
my paranoia says "assume I mess up" ( I am good at messing up ) so trap the error before it happens.
I have looked everywhere i can think of and cannot find a way to do so ( or am missing it )
Can I test to see if "fp" is actualy alive so I can avoid trying to close it if it is not.
if so how?
not sure if important but:
using
gcc verion 4.4.3
make version 3.81
Many Thanks in advance
Dave