S
Sathyaish
If fopen fails, is there a way to know why?
Sathyaish said:If fopen fails, is there a way to know why?
If fopen fails, is there a way to know why?
If by "fails" you mean "returns NULL", yes. The global variable errno
(found in <errno.h>) contains information about what went wrong; you
can use perror() to print that information as a readable string.
Thanks for the clarification. For a moment there I was afraid I'd have to findClaiming that ANSI C does not allow a weird author at the keyboard is wrong.
Christopher said:If by "fails" you mean "returns NULL", yes. The global variable errno
(found in <errno.h>) contains information about what went wrong; you
can use perror() to print that information as a readable string.
[snip]Eric Sosman said:FYI, this is true of many C implementations but it
is *not* required by the Standard. Some people think
Eric Sosman said:FYI, this is true of many C implementations but it
is *not* required by the Standard.
In said:If by "fails" you mean "returns NULL", yes. The global variable errno
(found in <errno.h>) contains information about what went wrong; you
can use perror() to print that information as a readable string.
if it fails and errno is non-null
Dave said:Shouldn't that be "nonzero"? ('Ts an integer value and not a pointer,
after all...)
pete said:"null" just means "zero".
"null pointer" is a pointer,
"null character" is an integer.
In said:Shouldn't that be "nonzero"? ('Ts an integer value and not a pointer,
after all...)
Richard said:Null is more general than "zero", because...
...exactly. A null pointer need not be zero in any way
(as opposed to a null pointer _constant_).
But, depending on whether you mean '\0', or char a which happens to be
null, not necessarily an int.
Please elaborate on the semantic differences between "null" and "zero"
in the context of the value of errno.
Dan Pop said:Yes, but you must be careful about it. The correct scenario is:
reset errno, call fopen, if it fails and errno is non-null you can assume
that its value provide a clue about the failure of fopen and translate it
into a meaningful message (perror or strerror), otherwise you must report
a failure for an unknown reason.
S.Tobias said:I don't quite understand it. What is the purpose of setting errno to
zero before function call? If a function sets errno on error, it will
do so - doesn't matter what the previous value was.
"S.Tobias said:I don't quite understand it. What is the purpose of setting errno to
zero before function call? If a function sets errno on error, it will
do so - doesn't matter what the previous value was.
Jonathan Adams said:But fopen() is not required to set errno every time it returns NULL.
Ah, thanks! I think I see what I was missing.
If a function does not communicate an error via errno, it is allowed
to set errno to any value it wants (except zero).
If a function communicates errors though errno, it is required either
to set errno to an error value, or to leave errno with the same value
as before the call.
Please correct me if that's not right.
S.Tobias said:Ah, thanks! I think I see what I was missing.
If a function does not communicate an error via errno, it is allowed
to set errno to any value it wants (except zero).
If a function communicates errors though errno, it is required either
to set errno to an error value, or to leave errno with the same value
as before the call.
Please correct me if that's not right.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.