习题8.1:
#include <iostream>
#include <string>
using std::cin; using std::cout; using std::endl;
using std::istream;
using std::string;
istream &f(istream &is)
{
string s;
is >> s;
for (; !is.eof(); is >> s)
cout << s << endl;
is.clear();
return is;
}
int main()
{
f(cin);
getchar();
getchar();
return 0;
}
习题8.5:8.4用getline()处理即可一行一行读。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::cin; using std::cout; using std::endl;
using std::istream; using std::ifstream;
using std::string;using std::vector;
void func(const string &ofile,vector<string> &svec)
{
ifstream line(ofile);
if (line)
{
string s;
line >> s;
while (!line.eof())
{
svec.push_back(s);
line >> s;
}
}
else
{
cout << "no file" << endl;
}
}
int main()
{
vector<string> svec;
func("hhh.txt", svec);
if (!svec.empty())
for (auto beg = svec.cbegin(); beg != svec.cend(); ++beg)//此处该range for的,傻了
cout << *beg << endl;
getchar();
getchar();
return 0;
}
习题8.6:
#include <iostream>
#include <string>
#include <fstream>
#include "Sales_data.h"
using std::cin; using std::cout;
using std::endl; using std::string;
using std::ifstream;
int main(){
cout << "this is a test:" << endl;
string s = "hhh.txt";
ifstream in(s);
if (in)
{
cout << "Open file:" + s << endl;
Sales_data total;
if (read(in, total))
{
Sales_data trans;
while (read(in, trans))//到最后一条时 此处不是输入数据的地方,cin返回true
{
if (total.isbn() == trans.isbn())//可在else下加入cout测得trans.bookIsbn为空,返回0,进入else
total.combine(trans);
else
{
print(cout, total) << endl;//这样写可以保证最后一条数据正常显示
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cout << "特么的啥也没有?" << endl;
}
}
else
cout << "Cannot open file:" + s << endl;
getchar();
getchar();
return 0;
}
习题8.7-8.8:
#include <iostream>
#include <string>
#include <fstream>
#include "Sales_data.h"
using std::cin; using std::cout;
using std::endl; using std::string;
using std::ifstream; using std::ofstream;
int main(){
cout << "this is a test:" << endl;
string read_file = "售卖记录.txt", write_file = "统计结果.txt";
ifstream in(read_file);
ofstream out(write_file,ofstream::app);
if (in || out)
{
cout << "Open file: " + read_file + " & " + write_file << endl;
Sales_data total;
if (read(in, total))
{
Sales_data trans;
while (read(in, trans))//到最后一条时 此处不是输入数据的地方,cin返回true
{
if (total.isbn() == trans.isbn())//可在else下加入cout测得trans.bookIsbn为空,返回0,进入else
total.combine(trans);
else
{
print(out, total) << endl;//这样写可以保证最后一条数据正常显示
total = trans;
}
}
print(out, total) << endl;
}
else
{
cout << "特么的啥也没有?" << endl;
}
}
else
cout << "Cannot open file:" + read_file << endl;
getchar();
getchar();
return 0;
}
习题8.9:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::cin; using std::cout; using std::endl;
using std::istream; using std::ifstream;
using std::string;using std::vector;
using std::istringstream;
istream &f(istream &is)
{
string s;
while (is>>s)
cout << s << endl;
is.clear();
return is;
}
int main()
{
istringstream is("hhhh");
f(is);
getchar();
getchar();
return 0;
}
习题8.10:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using std::cin; using std::cout; using std::endl;
using std::istream; using std::ifstream;
using std::string;using std::vector;
using std::istringstream;
void file_to_vec(istream &infile, vector<string> &svec)
{
string s;
while (std::getline(infile, s))
svec.push_back(s);
}
void vec_to_string(const vector<string> &svec)
{
for (auto &line : svec)
{
istringstream record(line);
string word;
while (record >> word)
cout << word << endl;
}
}
int main()
{
string file_name("hhh.txt");
vector<string> svec;
ifstream in(file_name);
if (in)
{
file_to_vec(in, svec);
vec_to_string(svec);
}
else
cout << "Cannot open file: " + file_name << endl;
getchar();
getchar();
return 0;
}
习题8.13:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
using std::cin; using std::cout; using std::endl;
using std::istream; using std::ostream;
using std::string; using std::vector;
using std::ifstream; using std::ofstream;
using std::istringstream; using std::ostringstream;
struct PersonInfo
{
string name;
vector<string> phones;
};
bool valid(const string phone_number)
{
if (phone_number.size() == 11)
{
for (const auto &c : phone_number)
if (!std::isdigit(c))
return false;
return true;
}
else
return false;
}
string &format(string &nums)
{
nums.insert(3, "-");
nums.insert(8, "-");
return nums;
}
void read_file(istream &infile, vector<PersonInfo> &pvec)
{
string line, word;
while (getline(infile, line))
{
PersonInfo people;
istringstream record(line);
record >> people.name;
while (record >> word)
people.phones.push_back(word);
pvec.push_back(people);
}
}
void write_file(ostream &outfile, vector<PersonInfo> &pvec)
{
for (auto &entry : pvec)
{
ostringstream formatted, badNums;
for (auto &nums : entry.phones)
{
if (!valid(nums))
badNums << " " << nums;
else
formatted << " " << format(nums);
}
if (badNums.str().empty())
outfile << entry.name << " " << formatted.str() << endl;
else
outfile << "input error: " << entry.name << " invalid number(s):" << badNums.str() << endl;
}
}
int main()
{
string in_file("contacts_origin.txt"), out_file("contacts_formatted.txt");
ifstream in(in_file);
if (in)
cout << "open contacts_origin.txt" << endl;
ofstream out(out_file);
if (out)
cout << "open contacts_formatted.txt" << endl;
vector<PersonInfo> pvec;
read_file(in, pvec);
write_file(out, pvec);
getchar();
getchar();
return 0;
}