According to man page fclose(3)
:
RETURN VALUE
Upon successful completion 0 is returned. Otherwise,
EOF
is returned and the global variableerrno
is set to indicate the error. In either case any further access (including another call tofclose()
) to the stream results in undefined behavior.ERRORS
EBADF
The file descriptor underlyingfp
is not valid.The
fclose()
function may also fail and seterrno
for any of the errors specified for the routinesclose(2)
,write(2)
orfflush(3)
.
Of course fclose(NULL)
should fail but I expect that it to return with an errno
normally instead of dying by segmentation fault directly. Is there any reason of this behavior?
Thanks in advance.
UPDATE: I shall put my code here (I'm trying strerror()
, particularly).
FILE *not_exist = NULL;
not_exist = fopen("nonexist", "r");
if(not_exist == NULL){
printError(errno);
}
if(fclose(not_exist) == EOF){
printError(errno);
}