第八章 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 ,即 ...
随机推荐
- eclipse安装tomcat时只有locahost,不显示server name
Eclipseh中无法安装Tomcat,报错信息如下 Cannot create a server using the selected type 原因:以前安装的tomcat目录改变 解决方法: ...
- angular2或angular4中使用ckplayer播放rtmp和m3u8视频直播流
1. 下载ckpalyer整个包并导入, 将ckplayer放到src/assets/下 2. 引入ckplayer.js angular2中,在angular-cli.json中找到script,添 ...
- day 21继承
1.了解Python2和python3类的区别: python2.3之前使用的是经典类, 在2.3版本之后组,使用的是新式类 MRO: method resolution order 方法的查找 ...
- python新手之字典增删改查
一.字典的定义 city_list = { 'beijin':"北京",'shanghai':"上海" } print(city_list) 二.字典添加一个元 ...
- pyhton 下 使用getch(), 输入字符无需回车
原代码来自 https://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin ...
- python 正则表达式 符号及其定义
较好的文章https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
- 数据结构之链表及其Java实现
数据的存储一般分线性存储结构和链式存储结构两种.前者是一种顺序的存储方式,在内存中用一块连续的内存空间存储数据,即逻辑上相连的物理位置相邻,比较常见的就是数组:后者是一种链式存储方式,不保证顺序性,逻 ...
- MYSQL和ORACLE的一些区别
有很多应用项目, 刚起步的时候用MYSQL数据库基本上能实现各种功能需求,随着应用用户的增多,数据量的增加,MYSQL渐渐地出现不堪重负的情况:连接很慢甚至宕机,于是就有把数据从MYSQL迁到ORAC ...
- 北京Uber优步司机奖励政策(1月1日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 上海Uber优步司机奖励政策(12月20日到12月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...