c – stdio是否总是设置errno?

当stdio流遇到错误(但不是EOF)时,流的错误指示器将被设置为使得ferror()将返回非零值.我一直假设在errno中有更多的信息可用.但是我怎么知道这个?

一些功能的文档[例如在Linux下的man fopen]说,errno也将被设置.然而,人类fgets根本没有提到errno. glibc信息页面令人放心:

In addition to setting the error indicator associated with the
stream,the functions that operate on streams also set `errno’ in the
same way as the corresponding low-level functions that operate on file
descriptors.

但我不知道这个保证是多么强大. C标准是否需要?在Visual C/C++中会发生什么?

解决方法

C标准本身不需要很多使用errno WRT到stdio函数;它指定ferror(),但只说明它

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.

从C99草案:http://www.vmunix.com/~gabor/c/draft.html.任何实际的错误代码,大部分是实现定义.

但是,linux上的GNU C库也符合POSIX规范:

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

在这种情况下,这些定义更加明确.例如,如果您查看fopen的页面

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

您会在错误之下看到很多详细的信息,包括特定的错误代码.

同样,在几乎所有正常的linux系统上使用的GNU C库都符合POSIX标准,因此您可以依赖该信息;).那些(在线)POSIX手册页也通常比标准linux系统手册页更详细(同时阅读两者).

WRT在其他(非POSIX)平台上进行文件操作,它们将具有自己的实现.不幸的是,这样的东西在标准C中不是透明的便携式的.但是,C流确实有更多的标准化错误处理.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...