条件状态知识点:
参考博客:https://www.cnblogs.com/yulianggo/p/9357175.html
iostate类型,是一种四个位集合类型:goodbit、badbit、eofbit、failbit,相应的函数good()、bad()、eof()、bad()返回bool类型的值,如果被置位,,则返回true,否则为false。
在我的机器里,和博主的不一样,rdstate 求解时返回状态__Mystate
enum _Iostate
{ // constants for stream states
_Statmask = 0x17};
static constexpr _Iostate goodbit = (_Iostate)0x0;
static constexpr _Iostate eofbit = (_Iostate)0x1;
static constexpr _Iostate failbit = (_Iostate)0x2;
static constexpr _Iostate badbit = (_Iostate)0x4;
_Mystate = (iostate)(_State & _Statmask);
这里对枚举类型赋值,我也不是很清楚,但是算法方面,求解状态:_Mystate = (iostate)(_State & _Statmask);可以得到流的状态;goodbit对应:0000,eofbit对应0001,failbit对应0010,badbit对应0100;
代码验证:
#include<iostream>
using namespace std;
void main()
{
int a;
cin >> a;
cout << "bad:" << endl;
cout << cin.badbit << endl;
cout << cin.bad() << endl;
cout << "fail:" << endl;
cout << cin.failbit << endl;
cout << cin.fail() << endl;
cout << "good:" << endl;
cout << cin.goodbit << endl;
cout << cin.good() << endl;
cout << "eof:" << endl;
cout << cin.eofbit << endl;
cout << cin.eof() << endl;
cout << cin.rdstate() << endl;
cin.clear(cin.goodbit);
cout << "bad:" << endl;
cout << cin.badbit << endl;
cout << cin.bad() << endl;
cout << "fail:" << endl;
cout << cin.failbit << endl;
cout << cin.fail() << endl;
cout << "good:" << endl;
cout << cin.goodbit << endl;
cout << cin.good() << endl;
cout << "eof:" << endl;
cout << cin.eofbit << endl;
cout << cin.eof() << endl;
cout << cin.rdstate() << endl;
cin.clear(~cin.goodbit);
cout << "bad:" << endl;
cout << cin.badbit << endl;
cout << cin.bad() << endl;
cout << "fail:" << endl;
cout << cin.failbit << endl;
cout << cin.fail() << endl;
cout << "good:" << endl;
cout << cin.goodbit << endl;
cout << cin.good() << endl;
cout << "eof:" << endl;
cout << cin.eofbit << endl;
cout << cin.eof() << endl;
cout << cin.rdstate() << endl;
}
习题8-1和习题8-2
#include<iostream>
#include<string>
using namespace std;
istream & func(istream &a)
{
string str;
while (!a.eof())
{
a >> str;
cout << str;
}
cout << endl;
a.clear();
return a;
}
void main()
{
cout<<func(cin).good()<<endl;
}
习题8-3
bad、eof、fail在对应错误位置时,cin会在错误状态,即循环中止;
习题8-4
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
void read(vector<string> &vec)
{
ifstream ifstrm;
string str;
ifstrm.open("data.txt");
while(!ifstrm.eof())
{
getline(ifstrm,str);
vec.push_back(str);
}
ifstrm.close();
}
void main()
{
vector<string> vstr;
read(vstr);
for (auto i = vstr.begin(); i != vstr.end(); ++i)
cout << *i << endl;
}
习题8-5
将getline(ifstrm,str)改成ifstrm>>str
习题8-6
打开一个文件,将cin替换成ifstrem就行;
int main(int argc, char** argv)
{
ifstream ifs(argv[1]);
if (ifs) {
Sales_data total;
if (read(ifs, total)) { // ifstream继承与istream,因此这里可以直接使用文件流。
Sales_data trans;
while (read(ifs, trans)) {
if (total.isbn() == trans.isbn()) {
total.combine(trans);
}
else {
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else {
cout << "No data?" << endl;
}
}
else {
cout << "file name error?" << endl;
}
return 0;
}
习题8-7
将ofstrem替换cout
int main(int argc, char** argv)
{
ifstream ifstrm(argv[0]);
ofstrem ofs(argv[1]);
if (ifs) {
Sales_data total;
if (read(ifs, total)) { // ifstream继承与istream,因此这里可以直接使用文件流。
Sales_data trans;
while (read(ifs, trans)) {
if (total.isbn() == trans.isbn()) {
total.combine(trans);
}
else {
print(ofs, total) << endl;
total = trans;
}
}
print(ofs, total) << endl;
}
else {
cout << "No data?" << endl;
}
}
else {
cout << "file name error?" << endl;
}
return 0;
}
习题8-8
将 ofstrem ofs(argv[1]);改成 ofstrem ofs(argv[1],ofstrem::app);
习题8-9
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
istream & func(istream &a)
{
string str;
while(!a.eof())
{
a >> str;
cout << str;
}
cout << endl;
a.clear();
return a;
}
void main()
{
istringstream iss("hello! how are you?");
cout << func(iss).good() << endl;
}
习题8-10
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
using namespace std;
void main()
{
ifstream ifs;
ifs.open("data.txt");
vector<string> strv;
string line,word;
getline(ifs, line);
ifs.close();
istringstream iss(line);
while (iss >> word)
strv.push_back(word);
for (auto i = strv.begin(); i != strv.end(); ++i)
cout << *i << endl;
}