书285页,练习8.4
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream in("data.txt");
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
string line;
vector<string> words;
while (getline(in, line))
{
words.push_back(line);
}
in.close();
vector<string>::const_iterator it = words.begin();
while (it != words.end())
{
cout << *it << " ";
++it;
}
}
8.6+8.7将输入文件名作为第一个参数,传给main函数,将输出文件名作为第二个参数传给main函数。
#include <iostream>
#include <fstream>
#include "Sales_data.h"
using namespace std;
int main(int argc,char *argv[])
{
if(argc != 3)
{
cerr << "请给出输入,输出文件名" << endl;
return -1;
}
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
ofstream out(argv[2]);
if (!out)
{
cerr << "无法打开输出文件" << endl;
return -1;
}
Sales_data total;
if (read(in, total))
{
Sales_data trans;
while (read(out, trans))
{
if (total.isbn() == trans.isbn())
total.combine(trans);
else
{
print(out, total) << endl;
total = trans;
}
}
print(out, total) << endl;
}
else
cerr << "No data" << endl;
}
练习8.10,将来自一个文件中的行保存在一个vector<string>中,然后使用一个istringstream从vector读取数据元素,每次读取一个单词
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main(int argc,char *argv[])
{
ifstream in("data.txt");
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
string line;
vector<string> words;
while (getline(in, line))
{
words.push_back(line);
}
in.close();
vector<string>::const_iterator it = words.begin();
while (it != words.end())
{
istringstream line_str(*it);
string word;
while (line_str >> word)
{
cout << word << " ";
++it;
}
}
}
8.11
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
struct PersonInfo
{
string name;
vector<string> phones;
};
int main(int argc,char *argv[])
{
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(cin, line))
{
PersonInfo info;//创建一个保存此数据的对象
record.clear();//重复使用字符串流的时候,每次调用clear
record.str(line);//将记录绑定到刚刚读入的行
record >> info.name;//读取名字
while (record >> word)//保持
{
info.phones.push_back(word);
}
people.push_back(info);
}
}
8.13
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
struct PersonInfo
{
string name;
vector<string> phones;
};
string format(const string &s){ return s; }
bool valid(const string &s)
{
//
//
return true;
}
int main(int argc,char *argv[])
{
string line, word;
vector<PersonInfo> people;
istringstream record;
if (argc != 2)
{
cerr << "请给出文件名" << endl;
return -1;
}
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
while (getline(cin, line))
{
PersonInfo info;//创建一个保存此数据的对象
record.clear();//重复使用字符串流的时候,每次调用clear
record.str(line);//将记录绑定到刚刚读入的行
record >> info.name;//读取名字
while (record >> word)//保持
{
info.phones.push_back(word);
}
people.push_back(info);
}
ostringstream os;
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())
{
os << entry.name << " " << formatted.str() << endl;
}
else
{
cerr << "input error:" << entry.name << "invalid number(s)" << badNums.str() << endl;
}
cout << os.str() << endl;
}
}
}