C ++ ifstream failbit和badbit [英] C++ ifstream failbit and badbit

查看:1167
本文介绍了C ++ ifstream failbit和badbit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中 ifstream 的情况下,在 failbit badbit flags set?

In case of ifstream in C++, under what conditions are failbit and badbit flags set ?

推荐答案

根据 cplusplus.com


<与操作本身的内部逻辑相关,因此流上的其他操作是可能的。虽然 badbit 通常在错误涉及流的完整性丢失时设置,即使对流执行不同的操作,其仍可能持续。可以通过调用成员函数bad独立检查badbit。

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.

strong> number ,当它检索字母时, 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 library

最后 strong>正确的解决方案如何处理所有错误位和异常,同时从文件(或访问一些文件或目录),我强烈建议您阅读一个非常全面和写好的文章使用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.

这篇关于C ++ ifstream failbit和badbit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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