8.1 && 8.2
#include <iostream>
using namespace std;
istream& func(istream& is)
{
string s;
while (is >> s)
{
cout << s << endl;
}
cout << is.rdstate() << endl;
is.clear();
return is;
}
int main()
{
istream& is = func(cin);
cout << is.rdstate() << endl;
}
8.3
一个流一旦发生错误,其上后续的IO操作都会失败。
如果badbit、failbit和eofbit任一个被置位,则检测流状态的条件会失败。
8.4
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
fstream fstrm;
fstrm.open("data.txt", fstream::in | fstream::app);
vector<string> data;
string str;
if (fstrm)
{
while (getline(fstrm, str))
{
data.push_back(str);
}
}
else {
cout << "file open error!" << endl;
return -1;
}
for (const auto &i:data)
{
cout << i << endl;
}
}
8.5
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
fstream fstrm;
vector<string> data;
string str;
fstrm.open("data.txt", fstream::in | fstream::app);
if (fstrm)
{
while (fstrm >> str)
{
data.push_back(str);
}
}
else {
cout << "file open error!" << endl;
return -1;
}
for (const auto &i:data)
{
cout << i << endl;
}
}
8.6
int main(int argc, char** argv)
{
ifstream input(argv[1]);
Sales_data total;
if (read(input, total))
{
Sales_data trans;
while (read(input, trans)) {
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else {
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else {
cerr << "No data?!" << endl;
}
}
8.7
int main(int argc, char** argv)
{
ifstream input(argv[1]);
ofstream output(argv[2]);
Sales_data total;
if (read(input, total))
{
Sales_data trans;
while (read(input, trans)) {
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else {
print(output, total) << endl;
total = trans;
}
}
print(output, total) << endl;
}
else {
cerr << "No data?!" << endl;
}
}
8.8
int main(int argc, char** argv)
{
ifstream input(argv[1]);
ofstream output(argv[2],ofstream::app);
Sales_data total;
if (read(input, total))
{
Sales_data trans;
while (read(input, trans)) {
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else {
print(output, total) << endl;
total = trans;
}
}
print(output, total) << endl;
}
else {
cerr << "No data?!" << endl;
}
}
8.9
#include <iostream>
#include <sstream>
using namespace std;
istream& func(istream& is)
{
string s;
while (is >> s)
{
cout << s << endl;
}
cout << is.rdstate() << endl;
is.clear();
return is;
}
int main()
{
istringstream str("hello world\n");
istream& is = func(str);
cout << is.rdstate() << endl;
}
8.10
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
istringstream& Read(vector<string>& vs, istringstream& is)
{
for (auto &i: vs)
{
is.str(i);
string temp;
while (is >> temp)
{
cout << temp << endl;
}
is.clear();
}
return is;
}
int main(int argc, char** argv)
{
ifstream input(argv[1]);
vector<string> vs;
string temp;
istringstream is;
if (input)
{
while (getline(input, temp))
{
vs.push_back(temp);
}
Read(vs, is);
cout << is.rdstate() << endl;
}
else {
cerr << "open file error!" << endl;
}
}
8.11
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
int main()
{
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(cin, line))
{
PersonInfo info;
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
record.clear();
people.push_back(info);
}
for (const auto& pe : people)
{
cout << pe.name;
for (const auto& ph : pe.phones)
cout << " " << ph;
cout << endl;
}
}
8.12
使用聚合类已满足要求。
不确定每个人号码的数量,无需使用类内初始化。
8.13
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
bool valid(const string& str)
{
return isdigit(str[0]); //首字符不是数字则无效
}
string format(const string& str) //号码格式化
{
return str;
}
int main(int argc, char** argv)
{
ifstream input(argv[1]);
string line, word;
vector<PersonInfo> people;
while (getline(input, line))
{
PersonInfo info;
istringstream record(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (const auto& entry : people)
{
ostringstream formatted, badNums;
for (const auto& nums : entry.phones)
{
if (!valid(nums))
badNums << " " << nums;
else
formatted << " " << format(nums); //号码格式化
}
if (badNums.str().empty())
cout << entry.name << " " << formatted.str() << endl; //输出名字及格式化的号码
else
cerr << "input error: " << entry.name
<< " invalid number(s) " << badNums.str() << endl;
}
}
8.14
- 它们都是类类型,因此使用引用避免拷贝
- 在循环当中不会改变它们的值,因此用 const