练习8.1 && 练习8.2:
#include<iostream>
#include<string>
using namespace std;
istream& func(istream& is)
{
string str;
while (is >> str)
cout << str << endl;
is.clear();
return is;
}
int main()
{
istream& is = func(cin);
cout << is.rdstate() << endl;
return 0;
}
练习8.3:
将cin置于错误状态导致终止。例如eofbit、failbit和badbit。
练习8.4:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string line;
while (getline(ifs, line))
vec.push_back(line);
}
}
int main()
{
vector<string> vec;
ReadFileToVec("data.txt", vec);
for (const string& elem elem : vec)
cout << elem << endl;
return 0;
}
练习8.5:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string line;
while (ifs >> line)
vec.push_back(line);
}
}
int main()
{
vector<string> vec;
ReadFileToVec("data.txt", vec);
for (const string& elem : vec)
cout << elem << endl;
return 0;
}
练习8.6:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#include"Sales_data.h"
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;
return -1;
}
return 0;
}
练习8.7:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#include"Sales_data.h"
int main(int argc, char** argv)
{
ifstream input(argv[1]); // 命令参数为
ofstream output(argv[2]); // 文件名1 文件名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;
return -1;
}
return 0;
}
练习8.8:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#include"Sales_data.h"
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;
return -1;
}
return 0;
}
练习8.9:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
istream& func(istream& is)
{
string str;
while (is >> str)
cout << str << endl;
is.clear();
return is;
}
int main()
{
istringstream iss("Hello Cpp");
func(iss);
return 0;
}
练习8.10:
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;
int main()
{
ifstream ifs("data.txt");
if (!ifs)
{
cerr << "No Data?" << endl;
return -1;
}
vector<string> vec;
string line;
while (getline(ifs, line))
{
vec.push_back(line);
}
for (const auto& elem : vec)
{
istringstream iss(elem);
string word;
while (iss >> word)
cout << word << endl;
}
return 0;
}
练习8.11:
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
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.clear(); // 将流的状态设置为有效
record.str(line); // 将记录绑定到刚读入的行
record >> info.name; // 读取名字
while (record >> word)
info.phones.push_back(word);// 读取号码
people.push_back(info); // 将此记录追加到people
}
for (const auto& info : people)
{
cout << info.name << " ";
for (const auto& phone : info.phones)
cout << phone << " ";
cout << endl;
}
return 0;
}
练习8.12:
因为我们需要一个聚合类。所以没有使用类内初始化。
练习 7.13:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<sstream>
using namespace std;
struct PersonInfo
{
string name;
vector<string> phones;
};
bool valid(const string& str);
string format(const string& str);
void read_record(istream& is, vector<PersonInfo>& people);
void output_record(ostream& os, const vector<PersonInfo>& people);
int main()
{
string filename;
cout << "Please input a record file name: ";
cin >> filename;
cout << endl;
ifstream ifs(filename);
if (!ifs)
{
cerr << "No data?" << endl;
return -1;
}
vector<PersonInfo> people;
read_record(ifs, people);
output_record(cout, people);
return 0;
}
void read_record(istream& is, vector<PersonInfo>& people)
{
string line, phone_Num; // 分别保存来自输入的一行和电话号码
while (getline(is, line))
{
PersonInfo info; // 创建一个保存此记录数据的对象
istringstream record(line); // 将记录绑定到刚读入的行
record >> info.name; // 读取名字
while (record >> phone_Num) // 读取电话号码
{
info.phones.push_back(phone_Num);
}
people.push_back(info);
}
}
void output_record(ostream& os, const vector<PersonInfo>& people)
{
// 使用ostringstream
for (const auto& entry : people) // 对people中每一项
{
ostringstream formatted, badNum; // 每个循环步创建对象
for (const auto& nums : entry.phones) // 对每个电话
{
if (!valid(nums))
{
badNum << " " << nums; // 将数的字符串形式存入badNum
}
else
{
// 将格式化的字符串写入formatted
formatted << " " << format(nums);
}
}
if (badNum.str().empty()) // 没有错误的电话
{
cout << entry.name << formatted.str() << endl; // 打印名字和格式化的电话
}
else // 打印名字和错误格式的电话
{
os << "error: " << entry.name << " invalid phone(s): " << badNum.str() << endl;
}
}
}
bool valid(const string& str)
{
for (auto& c : str)
{
if (!isdigit(c))
{
return false;
}
}
return true;
}
string format(const string& str)
{
return str.substr(0, 3) + "-" + str.substr(3, 3) + "-" + str.substr(6);
}
练习8.14:
因为它们都是类类型,而不是内置类型。所以引用更有效。
输出不应改变它们的值。所以我们增加了const。