使用c++ 的stream遇到的问题
2015-01-20 21:40
190 查看
c++的stream系列类是c++复杂性的一个体现点。总体来讲,复杂性安全性超过c,但速度低于c。
我是由于处理的数据量小,但数据形式不固定,而c++的stream有助于格式检查,所以使用了c++的stream。下面开始。
1.状态判断问题
我使用的代码大致是:
发现eof是文档结尾,所以str的结尾不会置上eofbit标志位,但由于出错,failbit位会被置上。
如果之后还要使用这个stringstream对象ss,一定要先ss.clear()将错误信息清空,stringstream不会自动清空。
我是由于处理的数据量小,但数据形式不固定,而c++的stream有助于格式检查,所以使用了c++的stream。下面开始。
1.状态判断问题
我使用的代码大致是:
string str; stringstream ss; ifstream infile; int a; ... getline(str, infile); ss.str(str); while(!ss.eof()) ss>>a;看起来无错,还把数依次读出来。但问题是每次while结束后,ss.fail()总是true. 经过翻看源码及文档
typedef unsigned int iostate; typedef iostate io_state; static const iostate badbit = 0x1; static const iostate eofbit = 0x2; static const iostate failbit = 0x4; static const iostate goodbit = 0x0;
eofbit | End-Of-File reached while performing an extracting operation on an input stream. |
failbit | The last input operation failed because of an error related to the internal logic of the operation itself. |
badbit | Error due to the failure of an input/output operation on the stream buffer. |
goodbit | No error. Represents the absence of all the above (the value zero). |
如果之后还要使用这个stringstream对象ss,一定要先ss.clear()将错误信息清空,stringstream不会自动清空。
相关文章推荐
- Linux下使用Eclipse编译C/C++代码时遇到问题的说明
- c++ 学习中遇到的问题: error C2065: 'ifstream' : undeclared identifier
- C/C++ 日常使用遇到的一些问题与解决
- C++中遇到同名函数问题,可以使用命名空间解决
- C++中使用new遇到的一个小问题
- linux Eclipse上c++调用使用了tensorflow库的python程序遇到的问题及解决方法
- 使用javac编译java文件和使用javah生成C/C++头文件 遇到找不到类的问题
- ubuntu 使用gcc 编译 C++ 及 遇到到问题
- VS2012 使用MySql的API函数连接数据库(成功), 以及使用mysql-connector-c++-1.1.3遇到的问题!
- 使用Code Blocks编写C++程序遇到的一些问题
- 在C++中调用C写的函数(使用lex_yacc遇到的问题)
- Qt中使用Xerces C++遇到的问题纪要
- ubuntu 使用cmake 编译 C++ 及 遇到到问题
- CrossApp开发时, 存放在Resource下的文件在android环境下使用assets/..路径 使用C++的文件操作类如(fopen、ifstream等)找不到的问题
- c++中使用std::map是遇到的小问题
- matlab文件编译成库在C++环境下使用遇到的问题记录
- (转)c++--使用CURL库在编译时遇到的问题
- c++ 学习中遇到的问题:ifstream打开指定路径时,显示路劲错误
- 使用Eclipse集成SDK时C++遇到的问题及解决
- ACE与C++/CLI集成使用时遇到的问题及解决方法