哪些事件可能导致错误返回非零值? [英] What events can cause ferror to return non-zero?

查看:146
本文介绍了哪些事件可能导致错误返回非零值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些事件可能导致 ferror()返回非零值,并且在哪些事件之后应该检查 ferror()

What events can cause ferror() to return non-zero and after what events should one check ferror()?

http:// www .cplusplus.com / reference / cstdio / ferror /

打开,阅读,关闭?

ferror()的返回值会自发改变吗?例如,如果程序检查 ferror(stream),则小睡片刻而不与与<$相关的 FILE 对象进行交互c $ c> stream ,然后再次检查 ferror(stream),返回值是否会有所不同?

Will the return value of ferror() ever change spontaneously? E.g., if a program checks ferror(stream), takes a nap without interacting with the FILE object associated with stream, and then checks ferror(stream) again, will the return value ever be different?

其中任何一项是由标准强制执行的吗?

Is any of this mandated by standards?

推荐答案

主要是底层系统返回的错误调用(例如 read write lseek close ),这将导致流的错误位被设置。

It is primarily errors returned from the underlying system calls (e.g. read, write, lseek, close) that will cause the stream's error bit to be set.

很多 f ___()< stdio.h 中的/ code>函数表明,如果到达文件末尾或发生错误,则 ferror() feof()将说明原因。 fscanf ,例如:

Many f___() functions from stdio.h indicate that if the end-of-file is reached, or an error occurs, ferror() or feof() will indicate why. fscanf, for example:


如果输入结束,则返回值 EOF
首次成功转换或匹配失败发生之前到达。如果发生读取错误,也会返回
EOF ,在这种情况下,流的错误
指示符(请参见 ferror( 3)设置),并设置 errno
表示错误。

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.

stdio.h 中的函数是同步的-没有后台线程在做什么,所以不,错误位(从<$返回c $ c> ferror())永远不会自发改变。

Functions from stdio.h are synchronous - there's no background thread doing anything, so no, the error bit (returned from ferror()) will never spontaneously change. It will only be affected by a call to libc by your application.

对于非常好奇的人,可以克隆GLibc。 Git存储库( git://sourceware.org/git/glibc.git )并查看代码本身。

For the very curious, one could clone the GLibc Git repository (git://sourceware.org/git/glibc.git) and have a look at the code itself.

ferror()本质上只是检查 _IO_ERR_SEEN 文件的 _flags 字段中的位。 grep 表示该常数将向您显示已设置/清除的所有位置。

ferror() essentially just checks for the _IO_ERR_SEEN bit in the file's _flags field. greping for that constant will show you all of the places it is set/cleared.

这篇关于哪些事件可能导致错误返回非零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆