8.1
istream& read(istream& i) {
string s;
while (i >> s) {
//...
}
i.clear();
return i;
}
8.2略
8.3
cin流的状态处于错误,比如eofbit,badbit,failbit
8.4
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (std::getline(ifs, buf))//fstream也可以用getline
vec.push_back(buf);
}
}
8.5
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (ifs >> buf);
vec.push_back(buf);
}
}
8.6
记得保存好自己的Sales_data,因为这本书可惦记这个类了。
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 0;
}
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;
}
return 0;
}
8.8
int main(int argc, char **argv)
{
ifstream input(argv[1]);
ofstream output(argv[2],ofstream::app);//默认是out,所以只写了一个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 0;
}
8.9
istream& read(istream& i) {
string s;
while (i >> s) {
cout << s << endl;
}
i.clear();
return i;
}
int main()
{
istringstream is("ds d s d");
read(is);
return 0;
}
结果如下
8.10
void read(string pathname) {
vector<string> vec;
fstream f(pathname, ifstream::in);
if (!f) {
return;
}
string s;
while (getline(f,s))
vec.push_back(s);
for (const auto& str : vec) {
istringstream ss(str);
string word;
while(ss >> word)
cout << word << " ";
}
}
8.11
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);
}
8.12
因为需要的是一个聚合类,所以不应该使用类内初始化。
8.13
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
using std::vector; using std::string; using std::cin; using std::istringstream;
using std::ostringstream; using std::ifstream; using std::cerr; using std::cout; using std::endl;
using std::isdigit;
struct PersonInfo {
string name;
vector<string> phones;
};
bool valid(const string& str)
{
return isdigit(str[0]);
}
string format(const string& str)
{
return str.substr(0,3) + "-" + str.substr(3,3) + "-" + str.substr(6);
}
int main()
{
ifstream ifs("../data/phonenumbers.txt");
if (!ifs)
{
cerr << "no phone numbers?" << endl;
return -1;
}
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(ifs, line))
{
PersonInfo info;
record.clear();
record.str(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;
}
return 0;
}
8.14
因为我们不会修改它们,所以定义成const