文章目录
读书笔记,以读书为目标,为了减少记笔记时间,重要内容已截图为主。
一、概述
二、内容
8.1、IO类
IO对象无拷贝和赋值
查询流的状态
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include <sstream>
using std::istringstream;
#include <string>
using std::string;
void read()
{
// turns on both fail and bad bits
cin.setstate(cin.badbit | cin.eofbit | cin.failbit);
}
void off()
{
// turns off failbit and badbit but all other bits unchanged
cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit);
}
int main()
{
cout << "before read" << endl;
if (cin.good()) cout << "cin's good" << endl;
if (cin.bad()) cout << "cin's bad" << endl;
if (cin.fail()) cout << "cin's fail" << endl;
if (cin.eof()) cout << "cin's eof" << endl;
read();
cout << "after read" << endl;
if (cin.good()) cout << "cin's good" << endl;
if (cin.bad()) cout << "cin's bad" << endl;
if (cin.fail()) cout << "cin's fail" << endl;
if (cin.eof()) cout << "cin's eof" << endl;
off();
cout << "after off" << endl;
if (cin.good()) cout << "cin's good" << endl;
if (cin.bad()) cout << "cin's bad" << endl;
if (cin.fail()) cout << "cin's fail" << endl;
if (cin.eof()) cout << "cin's eof" << endl;
return 0;
}
管理条件状态
#include <iostream>
using std::endl; using std::flush; using std::ends;
using std::unitbuf; using std::nounitbuf; using std::cout;
int main()
{
// writes hi and a newline, then flushes the buffer
cout << "hi!" << endl;
// writes hi, then flushes the buffer; adds no data
cout << "hi!" << flush;
// writes hi and a null, then flushes the buffer
cout << "hi!" << ends;
cout << unitbuf; // all writes will be flushed immediately
// any output is flushed immediately, no buffering
cout << "first" << " second" << endl;
cout << nounitbuf; // returns to normal buffering
return 0;
}
unitbuf操作符
tie
8.2、文件的输入输出
#include <iostream>
using std::cerr; using std::cout; using std::endl;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <stdexcept>
using std::runtime_error;
void process(ifstream &is)
{
string s;
while (is >> s)
cout << s << endl;
}
int main(int argc, char* argv[])
{
// for each file passed to the program
for (auto p = argv + 1; p != argv + argc; ++p) {
ifstream input(*p); // create input and open the file
if (input) { // if the file is ok, ``process'' this file
process(input);
} else
cerr << "couldn't open: " + string(*p);
} // input goes out of scope and is destroyed on each iteration
auto p = argv + 1, end = argv + argc;
ifstream input;
while (p != end) { // for each file passed to the program
input.open(*p); // open the file, automatically clears the stream
if (input) { // if the file is ok, read and ``process'' the input
process(input);
} else
cerr << "couldn't open: " + string(*p);
input.close(); // close file when we're done with it
++p; // increment pointer to get next file
}
}
自动构造和析构
文件模式
以out打开文件会丢弃已有数据
8.3、string 流
#include <iostream>
using std::cin; using std::cout; using std::cerr;
using std::istream; using std::ostream; using std::endl;
#include <sstream>
using std::ostringstream; using std::istringstream;
#include <vector>
using std::vector;
#include <string>
using std::string;
// members are public by default
struct PersonInfo {
string name;
vector<string> phones;
};
// we'll see how to reformat phone numbers in chapter 17
// for now just return the string we're given
string format(const string &s) { return s; }
bool valid(const string &s)
{
// we'll see how to validate phone numbers
// in chapter 17, for now just return true
return true;
}
vector<PersonInfo>
getData(istream &is)
{
// will hold a line and word from input, respectively
string line, word;
// will hold all the records from the input
vector<PersonInfo> people;
// read the input a line at a time until end-of-file (or other error)
while (getline(is, line)) {
PersonInfo info; // object to hold this record's data
istringstream record(line); // bind record to the line we just read
record >> info.name; // read the name
while (record >> word) // read the phone numbers
info.phones.push_back(word); // and store them
people.push_back(info); // append this record to people
}
return people;
}
ostream& process(ostream &os, vector<PersonInfo> people)
{
for (const auto &entry : people) { // for each entry in people
ostringstream formatted, badNums; // objects created on each loop
for (const auto &nums : entry.phones) { // for each number
if (!valid(nums)) {
badNums << " " << nums; // string in badNums
} else
// ``writes'' to formatted's string
formatted << " " << format(nums);
}
if (badNums.str().empty()) // there were no bad numbers
os << entry.name << " " // print the name
<< formatted.str() << endl; // and reformatted numbers
else // otherwise, print the name and bad numbers
cerr << "input error: " << entry.name
<< " invalid number(s) " << badNums.str() << endl;
}
return os;
}
int main()
{
process(cout, getData(cin));
return 0;
}