第八章 IO库
8.1
istream& func(istream &is)
{
std::string buf;
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}
8.2
#include <iostream>
using std::istream;
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;
}
8.3
badbit(系统级错误) failbit(可恢复错误) eofbit(到达文件结束符)
8.4
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using std::vector;
using std::string;
using std::ifstream;
using std::cout;
using std::endl;
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("../data/book.txt", vec);
for (const auto& str : vec) cout << str << endl;
return 0;
}
8.5
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using std::vector;
using std::string;
using std::ifstream;
using std::cout;
using std::endl;
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("../data/book.txt", vec);
for (const auto& str : vec) cout << str << endl;
return 0;
}
8.6
#include <fstream>
#include <iostream>
#include "../ch07/ex7_26_sales_data.h"
int main(int argc, char** argv)
{
std::ifstream input(argv[1]); // use "../data/book.txt"
Sales_data total;
if (read(input, total)) {
Sales_data trans;
while (read(input, trans)) {
if (total.isbn() == trans.isbn())
total.combine(trans);
else {
print(std::cout, total) << std::endl;
total = trans;
}
}
print(std::cout, total) << std::endl;
}
else {
std::cerr << "No data?!" << std::endl;
}
}
/*
*! Output:
*! 0-201-78345-X 5 110
*! 0-201-78346-X 9 839.2
*/
8.7
#include <fstream>
#include "../ch07/ex7_26_sales_data.h"
int main(int argc, char** argv)
{
std::ifstream input(argv[1]); // "../data/book.txt"
std::ofstream output(argv[2]); // "../data/out.txt"
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) << std::endl;
total = trans;
}
}
print(output, total) << std::endl;
}
else {
std::cerr << "No data?!" << std::endl;
}
}
8.8
#include <fstream>
#include "../ch07/ex7_26_sales_data.h"
int main(int argc, char** argv)
{
std::ifstream input(argv[1]); // "../data/book.txt"
std::ofstream output(argv[2], std::ofstream::app); // "../data/out.txt"
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) << std::endl;
total = trans;
}
}
print(output, total) << std::endl;
}
else {
std::cerr << "No data?!" << std::endl;
}
}
8.9
#include <iostream>
#include <sstream>
using std::istream;
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::ifstream;
using std::istringstream;
using std::cout;
using std::endl;
using std::cerr;
int main()
{
ifstream ifs("../data/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;
}
8.11
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::vector;
using std::string;
using std::cin;
using std::istringstream;
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;
}
8.12
因为PersonInfo在这里是一个聚合类。
聚合类:只含有公共成员的类,并且没有类内初始值和构造函数。聚合类的成员可以用花括号括起来的初始值列表进行初始化。
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
因为它们都是类类型,而不是内置类型。所以引用更有效。
输出不应改变它们的值。所以我们增加了常量。