• check if a state is ok

    while(cin >> word)
    // ok: read successful

  • condition states are constants defined in ios_base as public members
    • ios_base::badbit : corrupted.

      • set when the error involves the loss of integrity of the stream, which is likely to persist even if a different operation is attempted on the stream.
      • indicates a system-level failure, such as an unrecoverable read or write error.
      • usually not possible to use a stream once badbit has been set.
    • ios_base::failbit : failed.
      • set by an operation when the error is related to the internal logic of the operation itself; further operations on the stream may be possible.
      • is set after a recoverable error, such as reading a character when numeric data was expected.
      • often possible to correct such problems and continue using the stream.
    • ios_base::eofbit : hit end-of-file.
    • ios_base::goodbit : not in an error state.
      • indicating none of the other bits is set.
  • Note
    • At least one of failbit and badbit is set when an error occured during an input operation.
    • Failing to read due to reaching the end-of-file sets both eofbit and failbit.
    • fail() is a synonym of ios::operator!
  • checks
    • s.bad() : eofbit is set.
    • s.fail() : failbit or badbit is set.
    • s.eof() : eof is set.
    • s.good() : none of above three states is set.
    • s.is_open() :
    • s.clear() : reset all condition values in the stream to valid state.
  • operation on flags
    • retrieve : basic_ios::rdstate
    • set : basic_ios::setstate
  • about types:
    • std::ios_base::iostate is type for stream error state flags

References:

  1. http://www.cplusplus.com/reference/ios/ios/fail/
  2. http://www.cplusplus.com/reference/ios/ios/operator_not/
  3. http://stackoverflow.com/questions/6255339/checking-if-a-file-opened-successfully-with-ifstream
  4. http://www.cplusplus.com/forum/beginner/6033/
  5. http://www.cplusplus.com/doc/tutorial/files/

right way check file open and end的更多相关文章

  1. 电脑每次开机都出现check file system on:C 的解决办法

    电脑每次开机都出现check file system on:C 的解决办法... ----------------------------------------- ----------------- ...

  2. windows安装dcm4chee 出错 check file system group LOSSY_STORAGE for deletion

    错误情景: 解决方法: 更改服务的监听端口(参考DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1)

  3. 12 Useful “df” Commands to Check Disk Space in Linux

    On the internet you will find plenty of tools for checking disk space utilization in Linux. However, ...

  4. Analysis about different methods for reading and writing file in Java language

    referee:Java Programming Tutorial Advanced Input & Output (I/O) JDK 1.4+ introduced the so-calle ...

  5. error_Could not load file or assembly

    原文链接 Could you be missing the loaded assembly from your configuration file? Ensure you have somethin ...

  6. udev example -- detect usb and write test file

    之前学习了下Udev,就随便做了个测试小程序.....设计什么的也没考虑,就实现了一个基本功能,插入U盘,识别,循环检测到有特定文件后,就然后往U盘里面写数据,插拔多次,都能正常工作. 里面的warn ...

  7. Fix Scheduled Task Won’t Run for .BAT File

    Step 1: Check File/Folder Permissions The first step to fixing this issue is ensuring that the accou ...

  8. 使用Monit监控本地进程

    目前用它监控某些服务,失败自动重启,同时监控特定的日志文件,如果有变化,就发邮件报警 安装不细写了,网上好多 我先用cat /proc/version看了下我的系统是el6的,于是wget http: ...

  9. SSD框架训练自己的数据集

    SSD demo中详细介绍了如何在VOC数据集上使用SSD进行物体检测的训练和验证.本文介绍如何使用SSD实现对自己数据集的训练和验证过程,内容包括: 1 数据集的标注2 数据集的转换3 使用SSD如 ...

随机推荐

  1. css3之gradient

    radial-gradient The CSS radial-gradient() function creates an <image> which represents a gradi ...

  2. 改变HTML中超链接的显示样式

    更详细的内容请参考:http://www.w3school.com.cn/tags/tag_a.asp HTML中的代码如下: <a class="news_title" t ...

  3. 【干货】.NET开发通用组件发布(三) 简易数据采集组件

    组件介绍和合作开发 http://www.cnblogs.com/MrHuo/p/MrHuoControls.html 简易数据采集组件 怎么说他是一个简易的数据采集组件呢?因为由于时间仓促,缺少从某 ...

  4. 数值运算内建函数(core python programming 2nd edition 5.6.2)

    数值运算内建函数 函数  功能 abs(num) 返回 num 的绝对值 coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个元组的形式返回. divmod(num1, ...

  5. [C入门 - 游戏编程系列] 贪吃蛇篇(六) - 蛇实现

    这一篇是关于设置蛇的属性的,接上一篇(五). 设置蛇的速度,很简单,只要不是负数就行了. void SNK_SetSnakeSpeed(Snake *snake, int speed) { ) sna ...

  6. 逃离迷宫(HDU 1728 BFS)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. 简单的使用php多线程抓取网页

    PHP 利用 Curl Functions 可以完成各种传送文件操作,比如模拟浏览器发送GET,POST请求等等,受限于php语言本身不支持多线程,所以开发爬虫程序效率并不高,这时候往往需 要借助Cu ...

  8. C51变量的存储

    一.全局变量和局部变量 全局变量和局部变量的区别在于作用域的不同.此外还有静态全局变量和静态局部变量. 全局变量作用域为全局,在一个源文件中定义,其他的源文件也可以应用.在其他的源文件中使用exter ...

  9. InnoSetup XML操作函数

    用于InnoSetup 5 以上.对XML文件的操作,简化InnoSetup XML访问过程. 1. [代码]InnoSetup 5 脚本     { ======================== ...

  10. 【剑指offer】面试题26:复杂链表的复制

    题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点). 思路: 复制自身到下一个结点: 设置新结点的random指针: 分离链表. 注意:判 ...