Does stdio always set errno?

六月ゝ 毕业季﹏ 提交于 2019-12-01 15:32:41

The C Standard itself does not require much use of errno WRT to stdio functions; it specifies ferror() but says of it only that

7.13.10.3 The ferror function The ferror function tests the error indicator for the stream pointed to by stream. The ferror function returns nonzero if and only if the error indicator is set for stream.

from the C99 Draft: http://www.vmunix.com/~gabor/c/draft.html. Any actual error codes used are, for the most part, implementation defined.

However, the GNU C library on linux also conforms to POSIX specifications:

http://pubs.opengroup.org/onlinepubs/9699919799/toc.htm

Which are much more well defined in this context. For example, if you look at the page for fopen:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html

You'll see a lot of detailed information, including specific errno codes, under Errors.

Again, the GNU C library used on virtually all normal linux systems is POSIX compliant, so you can count on that information ;). Those (online) POSIX man pages are also generally more detailed than the standard linux system man pages (read both).

WRT to file operations on other (non-POSIX) platforms, they will have their own implementations. Unfortunately, stuff like that is not transparently portable in standard C. C++ streams do have more standardized error handling, though.

According to the C11 standard, chapter 7.21 ("stdio.h"), only fgetpos, fsetpos and ftell write to errno in the event of an error.

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