第八章 IO库
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库的更多相关文章
- [APUE]标准IO库(下)
一.标准IO的效率 对比以下四个程序的用户CPU.系统CPU与时钟时间对比 程序1:系统IO 程序2:标准IO getc版本 程序3:标准IO fgets版本 结果: [注:该表截取自APUE,上表中 ...
- [APUE]标准IO库(上)
一.流和FILE对象 系统IO都是针对文件描述符,当打开一个文件时,即返回一个文件描述符,然后用该文件描述符来进行下面的操作,而对于标准IO库,它们的操作则是围绕流(stream)进行的. 当打开一个 ...
- 文件IO函数和标准IO库的区别
摘自 http://blog.chinaunix.net/uid-26565142-id-3051729.html 1,文件IO函数,在Unix中,有如下5个:open,read,write,lsee ...
- 【转载】C++ IO库
本篇随笔为转载,原贴地址:<C++ Primer>第8章 IO库 学习笔记. 1.IO类 #include <iostream> istream//从流中读取数据 ostrea ...
- 从Decorator,Adapter模式看Java的IO库
我想任何一本介绍模式的书在讲到Decorator模式的时候不能不提到它的实际应用--在Java/IO库里面的应用,<<Java与模式>>这本书也不例外,有点不一样的是,这本书在 ...
- C++ Primer 读书笔记: 第8章 标准IO库
第8章 标准IO库 8.1 面向对象的标准库 1. IO类型在三个独立的头文件中定义:iostream定义读写控制窗口的类型,fstream定义读写已命名文件的类型,而sstream所定义的类型则用于 ...
- 标准模板库——IO库
IO库设施: . istream(输入流)类型,提供输入操作. . ostream(输出流)类型,提供输出操作. . cin,一个istream对象,从标准输入读取数据. . cout,一个ostre ...
- 高级UNIX环境编程5 标准IO库
标准IO库都围绕流进进行的 <stdio.h><wchar.h> memccpy 一般用汇编写的 ftell/fseek/ftello/fseeko/fgetpos/fsetp ...
- IO库
IO类 C++语言不直接处理出入输出,而是通过一族定义在标准库中的类型来处理IO.这些类型支持从设备读取数据.向设备写入数据的IO操作,设备可以是文件 .控制台窗口 等.还有一些类型允许内存IO ,即 ...
随机推荐
- Flask入门数据库的查询集与过滤器(十一)
1 查询集 : 指数据查询的集合 原始查询集: 不经过任何过滤返回的结果为原始查询集 数据查询集: 将原始查询集经过条件的筛选最终返回的结果 查询过滤器: 过滤器 功能 cls.query.filte ...
- 用RestTemplate调取接口,取得返回数据,携带header,动态拼接url ,动态参数
记录我自己的工作 get 请求 ,携带 请求头 header (token) url 根据参数 动态拼接 参数 放入 map 动态拼接 private String lclUrl = &quo ...
- 编译升级至openssh7.6
1.概述 目的:下载源码包(https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/openssh-7.6p1.tar.gz),编译升级为openssh为7.6 ...
- foreach传值
- CentOS6的/etc/rc.local不执行的问题解决
小编在一个场景下,希望java -jar abc.jar命令在系统开机自启动是自动执行,于是乎在文件/etc/rc.local中写入nohup java -jar abc.jar &,重启后发 ...
- Delphi采用接口实现DLL调用
Delphi使用模块化开发,可以采用DLL或者BPL,两者的区别是BPL只能被同版本的Delphi使用,DLL可以被不同版本和不同开发工具的开发的软件调用. 因此我们的软件大多使用Delphi作为界面 ...
- 第五节 Go数据结构之队列
一.什么是队列 数据结构里的队列就是模仿现实中的排队.如上图中狗狗排队上厕所,新来的狗狗排到队伍最后,最前面的狗狗撒完尿走开,后面的跟上.可以看出队列有两个特点: (1) 新来的都排在队尾: (2) ...
- FPGA烧完程序之后,检测不到网口的
原因:未给phy芯片添加复位 解决方法:在程序顶部添加一个输出信号output e_reset,使其值一直为高. output e_reset, 'b1;
- tomcat8.5配置高并发
最近部署的tomcat应用,有一天压测的时候,测试一致反馈下载不了,结果查看日志才发现如下错误: INFO: Maximum number of threads (200) created for c ...
- 北京Uber优步司机奖励政策(12月19日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...