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. 前台页面上传data image图片,java后台接收图片保存

    最近在项目中有这么一个需求,就是上传一个视频文件,然后要获取视频文件的第一帧图片,这个可以通过canvas获取得到,得到的是一个dataURL,之后还要将这个图片上传到云,这个时候如何操作就不清楚了, ...

  2. 30行代码实现js原生三级联动菜单

    var oneArr=[['00','成都'],['01','绵阳'],['02','南充']] var towArr={ '00':[['000','武侯区'],['002','锦江区']], '0 ...

  3. Python基础、条件语句和基本数据类型

    1. 第一句python - 后缀名是可以是任意? - 导入模块时,如果不是.py文件 ==> 以后文件后缀名是 .py 2. 两种执行方式 python解释器 py文件路径 python 进入 ...

  4. mfc和qt的区别

    注:引用来源 http://wenda.chinabaike.com/b/30934/2013/1208/707410.html QT使用的编译器是MinGW,即Linux下的GCC移植到window ...

  5. A1050

    输入两个字符串,将第一个字符串中包含的第二个字符串的字符去掉(包括空格),然后输出. gets()不能用了,我混搭了string和length(),不用纠结长度还是很好的. 第二个字符串所在HashT ...

  6. sublime解决gbk中文乱码包括Package Control: Install Package 无法使用

    最近喜欢上了sublime,打算抛弃notepad,但是发现sublime居然不支持gbk编码,再上网查找资料之后,总结了一套解决方法,目前为止是行之有效的. 日期:2019年3月14日 第一步:到G ...

  7. 简单R语言爬虫

    R爬虫实验 R爬虫实验 PeRl 简单的R语言爬虫实验,因为比较懒,在处理javascript翻页上用了取巧的办法. 主要用到的网页相关的R包是: {rvest}. 其余的R包都是常用包. libra ...

  8. 【转】Odoo装饰器: one装饰

    one装饰器的作用是对每一条记录都执行对应的方法,相当于traditional-style中的function,无返回值! 应用举例: 定义的columns now = fields.Datetime ...

  9. Eclipse安装Java Class反编译插件

    第一步:没有安装之前 第二步:从Eclipse Marketplace里,安装反编译插件jadclipse. 第三步:安装反编译插件之后,多了一个查看器,把"类反编译查看器"设置为 ...

  10. 北京Uber优步司机奖励政策(2月22日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...