8.1&&8.2

 #include <iostream>
#include <vector>
#include <string> using namespace std; istream &func(istream &is)
{
int val;
while (is >> val && !is.eof()) {
cout << val << endl;
}
is.clear(); //复位所有错误标志位
} int main()
{
func(cin);
return ;
}

8.3

读取类型不匹配;遇到文件结束符EOF;IO流错误

8.4

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; int main()
{
vector<string> vec;
string ifile = "data.txt", ss; //需在本文件夹下有data.txt这个文件
ifstream in(ifile);
while(getline(in, ss)) {
vec.push_back(ss);
}
in.close();
for (auto i : vec)
cout << i << endl;
return ;
}

8.5

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; int main()
{
vector<string> vec;
string ifile = "data.txt", ss; //需在本文件夹下有data.txt这个文件
ifstream in(ifile);
while(in >> ss) { //改动处
vec.push_back(ss);
}
in.close();
for (auto i : vec)
cout << i << endl;
return ;
}

8.6

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; struct Sales_data {
string bookNo; //书的ISBN
unsigned units_sold = ; //售出的本数
double revenue = 0.0; //销售额
Sales_data& combine(const Sales_data &);
string isbn() { return bookNo; }
}; Sales_data &Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} istream &read(istream &is, Sales_data &item)
{
is >> item.bookNo >> item.units_sold >> item.revenue;
return is;
} ostream &print(ostream &os, const Sales_data &item)
{
os << item.bookNo << " " << 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[])
{
if (argc != ) {
cerr<< "Please give the file name."<<endl;
return -;
}
ifstream in(argv[]);
if (!in) {
cerr<<"Can't open the file."<<endl;
return -;
}
Sales_data total;
if (read(in, total)) {
Sales_data trans;
while (read(in, trans)) {
if (total.isbn() == trans.isbn()) total = add(total, trans);
else {
print(cout, total);
cout << endl;
total = trans; //结构体赋值很方便
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
}
else {
cerr << "No data?!" << endl;
return -;
}
return ;
}

文件名(book.txt)通过参数传给main函数:

8.7

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; struct Sales_data {
string bookNo; //书的ISBN
unsigned units_sold = ; //售出的本数
double revenue = 0.0; //销售额
Sales_data& combine(const Sales_data &);
string isbn() { return bookNo; }
}; Sales_data &Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} istream &read(istream &is, Sales_data &item)
{
is >> item.bookNo >> item.units_sold >> item.revenue;
return is;
} ostream &print(ostream &os, const Sales_data &item)
{
os << item.bookNo << " " << 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[])
{
if (argc != ) {
cerr<< "Please give the input file name and output file name."<<endl;
return -;
}
ifstream in(argv[]);
ofstream out(argv[]);
if (!in) {
cerr<<"Can't open the file."<<endl;
return -;
}
if (!out) {
cerr<<"can't open output file."<<endl;
return -;
}
Sales_data total;
if (read(in, total)) {
Sales_data trans;
while (read(in, trans)) {
if (total.isbn() == trans.isbn()) total = add(total, trans);
else {
print(out, total) << endl; //输出到指定文件中
total = trans;
}
}
print(out, total) << endl;
}
else {
cerr << "No data?!" << endl;
return -;
}
return ;
}

将上一题输出的内容输出到文件book1.txt中:

8.8

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; struct Sales_data {
string bookNo; //书的ISBN
unsigned units_sold = ; //售出的本数
double revenue = 0.0; //销售额
Sales_data& combine(const Sales_data &);
string isbn() { return bookNo; }
}; Sales_data &Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} istream &read(istream &is, Sales_data &item)
{
is >> item.bookNo >> item.units_sold >> item.revenue;
return is;
} ostream &print(ostream &os, const Sales_data &item)
{
os << item.bookNo << " " << 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[])
{
if (argc != ) {
cerr<< "Please give the input file name and output file name."<<endl;
return -;
}
ifstream in(argv[]);
ofstream out(argv[], ofstream::app); //唯一改变之处
if (!in) {
cerr<<"Can't open the file."<<endl;
return -;
}
if (!out) {
cerr<<"can't open output file."<<endl;
return -;
}
Sales_data total;
if (read(in, total)) {
Sales_data trans;
while (read(in, trans)) {
if (total.isbn() == trans.isbn()) total = add(total, trans);
else {
print(out, total) << endl;
total = trans;
}
}
print(out, total) << endl;
}
else {
cerr << "No data?!" << endl;
return -;
}
return ;
}

8.9

 #include <iostream>
#include <vector>
#include <string>
#include <sstream> using namespace std; int main()
{
string line, word;
while(getline(cin, line)) {
istringstream in(line);
while (in >> word)
cout << word << endl;
}
return ;
}

8.10

 #include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream> using namespace std; int main()
{
vector<string> vec;
string line, word;
ifstream input("data.txt");
while(getline(input, line)) {
vec.push_back(line);
}
for (auto i : vec) {
istringstream in(i);
while (in >> word) {
cout << word << endl;
}
}
return ;
}

8.11

 #include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream> 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(); //每次要解除上次绑定的string对象
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
return ;
}

8.12

因为此时我们需要其中的成员皆可被访问,所以 PersonInfo 是一个聚合类,不能在内部进行初始化

8.13

 #include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream> using namespace std; struct PersonInfo {
string name;
vector<string> phones;
}; int main()
{
ifstream in("data.txt"); //从文件读取
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(in,line)) { //in为文件输入流
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
return ;
}

8.14

这两条语句分别适用范围for语句枚举people中所有项和每项的phones中的所有项。

使用const表明在循环中不会改变这些项的值;

使用auto是请求编译器依据vector元素类型来推断出entry和nums的类型,既简化代码又避免出错;

使用引用的原因是,people和phones的元素分别是结构对象和字符串对象,使用引用即可避免对象拷贝。

第八章 IO库的更多相关文章

  1. [APUE]标准IO库(下)

    一.标准IO的效率 对比以下四个程序的用户CPU.系统CPU与时钟时间对比 程序1:系统IO 程序2:标准IO getc版本 程序3:标准IO fgets版本 结果: [注:该表截取自APUE,上表中 ...

  2. [APUE]标准IO库(上)

    一.流和FILE对象 系统IO都是针对文件描述符,当打开一个文件时,即返回一个文件描述符,然后用该文件描述符来进行下面的操作,而对于标准IO库,它们的操作则是围绕流(stream)进行的. 当打开一个 ...

  3. 文件IO函数和标准IO库的区别

    摘自 http://blog.chinaunix.net/uid-26565142-id-3051729.html 1,文件IO函数,在Unix中,有如下5个:open,read,write,lsee ...

  4. 【转载】C++ IO库

    本篇随笔为转载,原贴地址:<C++ Primer>第8章 IO库 学习笔记. 1.IO类 #include <iostream> istream//从流中读取数据 ostrea ...

  5. 从Decorator,Adapter模式看Java的IO库

    我想任何一本介绍模式的书在讲到Decorator模式的时候不能不提到它的实际应用--在Java/IO库里面的应用,<<Java与模式>>这本书也不例外,有点不一样的是,这本书在 ...

  6. C++ Primer 读书笔记: 第8章 标准IO库

    第8章 标准IO库 8.1 面向对象的标准库 1. IO类型在三个独立的头文件中定义:iostream定义读写控制窗口的类型,fstream定义读写已命名文件的类型,而sstream所定义的类型则用于 ...

  7. 标准模板库——IO库

    IO库设施: . istream(输入流)类型,提供输入操作. . ostream(输出流)类型,提供输出操作. . cin,一个istream对象,从标准输入读取数据. . cout,一个ostre ...

  8. 高级UNIX环境编程5 标准IO库

    标准IO库都围绕流进进行的 <stdio.h><wchar.h> memccpy 一般用汇编写的 ftell/fseek/ftello/fseeko/fgetpos/fsetp ...

  9. IO库

    IO类 C++语言不直接处理出入输出,而是通过一族定义在标准库中的类型来处理IO.这些类型支持从设备读取数据.向设备写入数据的IO操作,设备可以是文件 .控制台窗口 等.还有一些类型允许内存IO ,即 ...

随机推荐

  1. Zabbix——设置阈值和报警

    前提条件: Zabbix 服务器可以正常监控其他设备 Zbbix 已经配置完成了邮件报警 Zabbix server版本为4.0 配置ICMP监测,1分钟如果ping不通,将会发送邮件 找到Templ ...

  2. python装饰器内获取函数有用信息方法

    装饰器内获取函数有用信息方法 .__doc__用于得到函数注释信息 .__name_用于得到函数名 在函数引用装饰器的时候,函数名会变为装饰器内部执行该函数的名字,所有在直接执行函数名加.__doc_ ...

  3. Flask之蓝图的使用

    蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...

  4. Vue.js的小例子--随便写的

    1.领导安排明天给同事们科普下vue 2.简单写了两个小例子 3.话不多说直接上代码 <!DOCTYPE html> <html> <head> <meta ...

  5. openWrt libubox组件之uloop原理分析

    1.    libubox概述 libubox是openwrt新版本中的一个基础库,有很多应用是基于libubox开发的,如uhttpd,netifd,ubusd等. libubox主要提供以下两种功 ...

  6. 如何将24位RGB颜色转换16位RGB颜色

    有许多朋友第一次使用16位彩色显示屏会遇到如何将24位RGB颜色转换为对应的16位RGB颜色的问题, 通过查阅相关资料,就写一下其中的转换原理吧,希望对大家会有所帮助. 我们知道24位RGB是分别由8 ...

  7. 前端css之float浮动

    浮动的准则,先找前一个块标签,在确认有否清除浮动的条件或者是距离的情况下,如果这一行能摆得下,就继续紧贴前一个标签 如果摆不下,就会另起一行 浮动只有左边和右边 如果是块标签,设置浮动,先把displ ...

  8. 从零开始一个http服务器(四)-动态返回

    从零开始一个http服务器(四) 代码地址 : https://github.com/flamedancer/cserver git checkout step4 运行: make clean &am ...

  9. Java垃圾回收机制概述

    总览 本文会介绍垃圾回收的以下几个方面. 为什么要垃圾回收 在哪里回收 哪些对象需要回收 怎么回收 HotSpotJVM中有哪些具体的回收器可以直接用. 在开始讲垃圾回收之前,先通过一张图快速回忆一下 ...

  10. Vue.js核心概念

    # 1. Vue.js是什么? 1). 一位华裔前Google工程师(尤雨溪)开发的前端js库 2). 作用: 动态构建用户界面 3). 特点: * 遵循MVVM模式 * 编码简洁, 体积小, 运行效 ...