C++ Primer(第五版)|练习题答案与解析(第八章:IO库)
本博客主要记录C++ Primer(第五版)中的练习题答案与解析。
参考:C++ Primer
C++ Primer
练习题8.1
编写函数,接受一个istream &参数,返回值也是istream&。此函数必须从给定流中读取数据,直至遇到文件结束标识符时停止。它将读取的数据打印在标准输出上。完成这些操作后,在返回流之前,对流进行复位,使其处于有效状态。
练习题8.2
测试函数,调用cin。
#include <iostream>
#include <vector>
#include <string>
#include <initializer_list>
using namespace std;
istream& func(istream &is)
{
std::string buf;
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}
int main()
{
istream& is = func(std::cin);
std::cout << is.rdstate() << std::endl;
return 0;
}
测试:
abcd
abcd
efg
efg
^Z
0
练习题8.3
什么情况下,下面的while循环会终止。
while (cin >> i) /* … */
- 遇到输入结束符;
- 遇到输入错误,比如i是int型,却输入了字符。
练习题8.4
编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中。
#include <iostream>
#include <vector>
#include <string>
#include <initializer_list>
#include <fstream>
using namespace std;
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (std::getline(ifs, buf))
vec.push_back(buf);
}
}
int main()
{
vector<string> vec;
ReadFileToVec("book.txt", vec);
for (const auto &str : vec)
cout << str << endl;
return 0;
}
输出:
hello world //是book.txt中的内容
练习题8.5
重写上面的程序,将每个单词作为一个独立的元素进行存储。
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (ifs >> buf)
vec.push_back(buf);
}
}
int main()
{
vector<string> vec;
ReadFileToVec("book.txt", vec);
for (const auto &str : vec)
cout << str << endl;
return 0;
}
输出:
hello
world
练习题8.6
重写7.1.1节的书店程序,从一个文件中读取交易记录。将文件名作为参数传递给main。
同P284页一样。
练习题8.7
修改上一个程序,将结果保存在一个文件中。将输出文件名作为第二个参数传给main。
头文件不变。
主程序:
#include <fstream>
#include <iostream>
#include "test.h"
using namespace std;
// nonmember functions
// member functions.
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
// nonmember functions
std::istream &read(std::istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue;
return os;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
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;
}
测试:
C:\Users\Administrator\Desktop\Test\bin\Debug>test.exe input.txt output.txt
input.txt:
0-201-78345-X 5 110
0-201-78346-X 9 839.2
output.txt:
0-201-78345-X 5 550
0-201-78346-X 9 7552.8
练习题8.8
修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。
把上一题ofstream ofs(argv[2]);
改为ofstream ofs(argv[2], ofstream::app);
output.txt:
0-201-78345-X 5 550
0-201-78346-X 9 7552.8
0-201-78345-X 5 550
0-201-78346-X 9 7552.8
练习题8.9
使用8.1.2节第一个练习所编写的函数打印一个istringstream对象的内容。
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
istream& func(istream &is)
{
std::string buf;
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}
int main()
{
std::istringstream iss("hello");
func(iss);
return 0;
}
练习题8.10
编写程序,将来自一个文件中的行保存在一个vector中,然后使用一个istringstream从vector读取数据元素,每次读取一个单词。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream ifs("book.txt");
if (!ifs)
{
cerr << "No data?" << endl;
return -1;
}
vector<string> vecLine;
string line;
while (getline(ifs, line))
vecLine.push_back(line);
for (auto &s : vecLine)
{
istringstream iss(s);
string word;
while (iss >> word)
cout << word << endl;
}
return 0;
}
测试
hello
world
练习题8.11
编写程序,将来自一个文件中的行保存在一个vector中,然后使用一个istringstream从vector读取数据元素,每次读取一个单词。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
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);
}
for (auto &p : people)
{
std::cout << p.name << " ";
for (auto &s : p.phones)
std::cout << s << " ";
std::cout << std::endl;
}
return 0;
}
测试:
morgan 2015553216 8554846
^Z
morgan 2015553216 8554846
练习题8.12
我们为什么没有在PersonInfo中使用类内初始化?
因为这里我们使用了聚合类,不需要类内初始化。
练习题8.13
重写本节的电脑号码程序,从一个命名文件而非cin读取数据。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
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.substr(0,3) + "-" + str.substr(3,3) + "-" + str.substr(6);
}
int main()
{
ifstream ifs("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;
}
测试
lee 123-468-5465 231-322-1
bob 213-154-6546 201-325-454
练习题8.14
我们为什么要将entry和nums定义为const auto&。
使用引用是因为,都是类内string类型,引用可以避免拷贝,提高效率。
const是因为函数内不改变对象的值。