哪些事件可能导致错误返回非零值?
哪些事件可能导致 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 (seeferror(3)
) is set, anderrno
is set indicate the error.
stdio.h
中的函数是同步的-没有后台线程在做什么,所以不,错误位(从 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. grep
ing for that constant will show you all of the places it is set/cleared.