C++ ifstream failbit 和 badbit
在C++中ifstream
的情况下,failbit
和badbit
标志是在什么情况下设置的?
In case of ifstream
in C++, under what conditions are failbit
and badbit
flags set ?
推荐答案
根据cplusplus.com:
failbit 通常由输入操作设置,当错误与操作本身的内部逻辑相关时,因此可能对流进行其他操作.而 badbit 通常在错误涉及流完整性丢失时设置,即使对流执行不同的操作,这种情况也可能持续存在.badbit 可以通过调用成员函数 bad 来独立检查.
failbit is generally set by an input operation when the error was related to the internal logic of the operation itself, so other operations on the stream may be possible. While badbit is generally set when the error involves the loss of integrity of the stream, which is likely to persist even if a different operation is performed on the stream. badbit can be checked independently by calling member function bad.
在简单的词中,如果您在期望检索信件时得到一个数字,那就是failbit
.如果发生严重错误,这会破坏从流中读取的能力 - 这是一个badbit
.
In simple words, if you get a number when expect to retrieve a letter, it's failbit
. If a serious error happens, which disrupts the ability to read from the stream at all - it's a badbit
.
除了提到的标志,还有第三个非常相似――eofbit
.您可以使用以下几个函数检查状态:ios::fail
,ios::good
和 ios::bad
Except mentioned flags there is a third quite similar ― eofbit
. You can check the state using several functions: ios::fail
, ios::good
and ios::bad
您可以熟悉iostream 库 也在 MSDN 资源中.
And you can get familiar with iostream library at MSDN resource too.
最后,如果您在从文件读取(或访问某个文件或目录)时搜索如何处理所有错误位和异常的正确解决方案,我强烈建议您阅读一篇非常全面且写得很好的文章使用ifstream在C++中读取文件:正确处理badbit、failbit、eofbit和perror()",最后你会发现一些理想的解决方案.这篇文章确实值得一读.
Finally, if you search for the correct solution of how to handling all error bits and exceptions while reading from the file (or accessing some file or directory), I highly recommend you read a very comprehensive and well-written article "Reading files in C++ using ifstream: dealing correctly with badbit, failbit, eofbit, and perror()", at the end of which you will find a few Ideal solutions. The article is worth to read indeed.
相关文章