What kind of errors set “errno” to non-zero? Why does fopen() set “errno” while fputc() does not?

こ雲淡風輕ζ 提交于 2019-12-03 14:36:05

The documentation largely tells you which function can set which values in errno, but there are some rules you need to know:

  1. No library function sets errno to zero.
  2. It is only valid to test errno when the function has indicated that an error has occurred (and the function is documented to set errno).

The first point means that if you want to know whether, for example, you've gotten an error from strtol(), you have to set errno to 0 before calling it.

The second point is crucial; for example, on Solaris, after many I/O operations when the channel was not a terminal, the setting of errno would be ENOTTY (not a terminal). There wasn't an error; nothing had failed; but basing your subsequent actions on errno alone (and not on the status reported by the I/O operation) would lead you to think everything failed.

Thus, in your code, the fopen() call may leave errno as a non-zero value, even though it successfully creates the file stream. You have to use:

const char filename[] = "D:\\crypt.txt";
if ((pFile = fopen(filename, "r")) == 0)
{
    fprintf(stderr, "Failed to open %s for reading (%d: %s)\n",
            filename, errno, strerror(errno));
    ...return or exit...
}

Beware: if you need to call a function that can alter errno, capture the value early:

    int errnum = errno;
    fprintf(stderr, "Failed to open %s for reading (%d: %s)\n",
            filename, errnum, strerror(errnum));

And never declare errno yourself; always use #include <errno.h> to do it.

I'm not clear why your code is not getting an error on the fputc() call. On my Mac OS X 10.8.3 system, the equivalent code fails with errno set to 9 (EBADF) 'Bad file descriptor'.


Where is this documented? It's in the C standard, and reinforced by the POSIX standard.

ISO/IEC 9899:2011 §7.5 Errors <errno.h>

¶3 The value of errno in the initial thread is zero at program startup (the initial value of errno in other threads is an indeterminate value), but is never set to zero by any library function.202) The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard.

202) Thus, a program that uses errno for error checking should set it to zero before a library function call, then inspect it before a subsequent library function call. Of course, a library function can save the value of errno on entry and then set it to zero, as long as the original value is restored if errno’s value is still zero just before the return.

The wording in previous versions of the C standard did not mention threads but was otherwise similar.

Note that the description of fopen() in the C standard does not mention errno. Therefore, it is permitted to set errno by the C standard. By contrast, the mbsrtowcs() function is documented to set errno to EILSEQ; it probably can't set it to other values because the C standard says it shouldn't (though there's nothing much to stop an implementation from doing so if it has a better error for some condition).

POSIX 2008

The POSIX page for errno says:

Many functions provide an error number in errno, which has type int and is defined in <errno.h>. The value of errno shall be defined only after a call to a function for which it is explicitly stated to be set and until it is changed by the next function call or if the application assigns it a value. The value of errno should only be examined when it is indicated to be valid by a function's return value. Applications shall obtain the definition of errno by the inclusion of <errno.h>. No function in this volume of POSIX.1-2008 shall set errno to 0. The setting of errno after a successful call to a function is unspecified unless the description of that function specifies that errno shall not be modified.

It is unspecified whether errno is a macro or an identifier declared with external linkage. If a macro definition is suppressed in order to access an actual object, or a program defines an identifier with the name errno, the behavior is undefined.

The symbolic values stored in errno are documented in the ERRORS sections on all relevant pages.

The wording in previous versions was similar.

Is my use of errno to test if fopen() worked successfully,instead of "if(pFile==NULL)" unwise?

From 7.5 Errors/3 of the C99 standard:

The value of errno is zero at program startup, but is never set to zero by any library function.159) The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard.

So checking errno to determine success or failure of an operation is unwise as a function is permitted to, pessimistically, set the value of errno to indicate failure, even if there is none. Only query errno if a function fails (if fopen() returns NULL or fputc() returns EOF for example).

u__

Just looked into the manual pages to confirm that valid error numbers are all nonzero; errno is never set to zero by any system call or library function.

Check the Linux errno.h man page: I believe you should look at return value and then look at errno.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!