IO类对象不允许进行拷贝操作。

IO类中定义后一些函数和标志,可以用于访问和操作流的状态。

一旦流发生错误,后续IO操作都是失败的。

读写IO对象会改变IO对象的状态。

每个输出流都管理一个缓冲区。

缓冲被刷新的原因有如下几种:

1.程序正常结束。

2.缓冲区满

3.endl刷新缓冲区

4.使用操作符unitbuf

5.被关联到另一个流

输入或输出流可以被关联到另一个输出流,不允许关联到输入流。

每个流最多同时关联到一个流,一个流可以被多个流关联。

文件流是将抽象流与文件关联起来,对流的操作就能等同于对文件的操作了。

每个文件流都有默认的文件模式。

string流是将对string的操作等同于对内存IO操作,而不是具体string。

练习8.1:编写函数,接受一个istream&参数,返回值类型也是istream&。此函数须从给定流中读取数据,直至遇到文件结束标识时停止。它将读取的数据打印在标准输出上。完成这些操作后,在返回流之前,对流进行复位,使其处于有效状态。

istream& fun(istream &input)
{
string s;
while (true)
{
input >> s;
if (input.eof())
{
cout << s << '\n';
break;
}
input.clear(); //防止fail和bad
cout << s << '\t';
}
input.clear();
return input;
}

练习8.2:测试函数,调用参数为cin。

#include <iostream>

using namespace std;

istream& fun(istream &input)
{
string s;
while (true)
{
input >> s;
if (input.eof())
break;
else
input.clear();
cout << s << '\t';
}
input.clear();
return input;
} int main()
{
fun(cin);
return ;
}

练习8.3:什么情况下,下面的while循环会终止?

while (cin >> i)    /* ... */

当cin流bad、fail、eof时循环终止

练习8.4:编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中。

#include <iostream>
#include <fstream>
#include <vector> using namespace std; int main(int argc, char const *argv[])
{
vector<string> v;
ifstream in_file(argv[]);
if (in_file)
{
string s;
while (getline(in_file,s))
{
v.push_back(s);
}
}
else
{
cout << "no file?!" << endl;
}
return ;
}

练习8.5:重写上面的程序,将每个单词作为一个独立的元素进行存储。

#include <iostream>
#include <fstream>
#include <vector> using namespace std; int main(int argc, char const *argv[])
{
vector<string> v;
ifstream in_file(argv[]);
if (in_file)
{
string s;
while (in_file >> s)
{
v.push_back(s);
}
}
else
{
cout << "no file?!" << endl;
} for (auto i : v)
{
cout << i << ' ';
}
return ;
}

练习8.6:重写7.1.1节的书店程序(第229页),从一个文件中读取交易记录。将文件名作为一个参数传递给main(参见6.2.5节,第196页)。

#include <iostream>
#include <fstream> using namespace std; int main(int argc, char const *argv[])
{
ifstream input(argv[]);
Sales_data total;
if (input >> total)
{
Sales_data trans;
while ((input >> total))
{
if (total.isbn() == trans.isbn())
total.combine(trans);
else
{
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cerr << "No data?!" << endl;
}
return ;
}

练习8.7:修改上一节的书店程序,将结果保存到一个文件中。将输出文件名作为第二个参数传递给main函数。

#include <iostream>
#include <fstream> using namespace std; int main(int argc, char const *argv[])
{
ifstream input(argv[]);
ofstream output(argv[]);
Sales_data total;
if (input >> total)
{
Sales_data trans;
while ((input >> total))
{
if (total.isbn() == trans.isbn())
total.combine(trans);
else
{
output << total << endl;
total = trans;
}
}
output << total << endl;
}
else
{
cerr << "No data?!" << endl;
}
return ;
}

练习8.8:修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。

#include <iostream>
#include <fstream> using namespace std; int main(int argc, char const *argv[])
{
ifstream input(argv[]);
ofstream output(argv[], ofstream::app);
Sales_data total;
if (input >> total)
{
Sales_data trans;
while ((input >> total))
{
if (total.isbn() == trans.isbn())
total.combine(trans);
else
{
output << total << endl;
total = trans;
}
}
output << total << endl;
}
else
{
cerr << "No data?!" << endl;
}
return ;
}

练习8.9:使用你为8.1.2节(第281页)第一个练习所编写的函数打印一个istringstream对象的内容。

#include <iostream>
#include <sstream> using namespace std; istream& fun(istream& input)
{
string s;
while (true)
{
input >> s;
if (input.eof())
{
cout << s << '\n';
break;
}
input.clear(); //防止fail和bad
cout << s << '\t';
}
input.clear();
return input;
} int main(int argc, char const *argv[])
{
istringstream is("hello");
fun(is);
return ;
}

练习8.10:编写程序,将来自一个文件中的行保存在一个vector中。然后使用一个istringstream从vector读取数据元素,每次读取一个单词。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector> using namespace std; int main(int argc, char const *argv[])
{
vector<string> v;
string line, one;
ifstream in_file(argv[]);
if (in_file)
{
while (getline(in_file, line))
{
//cout << line << endl;
v.push_back(line);
}
} for (auto i : v)
{
istringstream in_string(i);
//cout << i << endl;
while (in_string >> one)
{
cout << one << ' ';
}
cout << endl;
}
cout << endl;
return ;
}

练习8.11:本节的程序在外层while循环中定义了istringstream 对象。如果record 对象定义在循环之外,你需要对程序进行怎样的修改?重写程序,将record的定义移到while循环之外,验证你设想的修改方法是否正确。

#include <iostream>
#include <sstream>
#include <vector> using namespace std; struct PersonInfo
{
string name;
vector<string> phones;
}; int main(int argc, char const *argv[])
{
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(cin, line))
{
PersonInfo info;
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
return ;
}

练习8.12:我们为什么没有在PersonInfo中使用类内初始化?
因为不需要类内初始化,PersonInfo的数据成员将默认初始化。

练习8.13:重写本节的电话号码程序,从一个命名文件而非cin读取数据。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector> using namespace std; struct PersonInfo
{
string name;
vector<string> phones;
}; string format(string)
{
return "format";
} bool valid(string)
{
int i;
return i % ;
} int main(int argc, char const * argv[])
{
ifstream in_file("~/people");
if (!in_file)
{
cerr << "no file?!" << endl;
return ;
}
string line, word;
vector<PersonInfo> people;
while (getline(in_file, line))
{
PersonInfo info;
istringstream record(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 ;
}

练习8.14:我们为什么将entry和nums定义为 const auto& ?
一是避免拷贝,二是防止改变它们。

C++ Primer 5th 第8章 IO库的更多相关文章

  1. C++ Primer 5th 第17章 标准库特殊设施

    C++新标准库提供了很多新功能,它们更加强大和易用. tuple类型 tuple是一种类似pair的模板,pair可以用来保存一对逻辑上有关联的元素对.但与pair不同的是,pair只能存储两个成员, ...

  2. 第 8 章 IO库

    第 8 章 IO库 标签: C++Primer 学习记录 IO库 第 8 章 IO库 8.1 IO类 8.2 文件输入输出 8.1 string流 8.1 IO类 IO对象无拷贝或赋值,因此不能将形参 ...

  3. 《C++ Primer》笔记 第8章 IO库

    iostream定义了用于读写流的基本类型,fstream定义了读写命名文件的类型,sstream定义了读写内存string对象的类型. 标准库使我们能忽略这些不同类型的流之间的差异,这是通过继承机制 ...

  4. 【c++ Prime 学习笔记】第8章 IO库

    C++语言不直接处理输入输出,而是通过标准库中的一组类来处理IO 1.2节介绍的IO库: istream(输入流)类型,提供输入 ostream(输出流)类型,提供输出 cin,是istream对象, ...

  5. 学习 primer 第8章 IO库 小结

    iostream处理控制台IO fstream处理命名文件IO stringstream完成内存string的IO 非常重要!!!!!!!!!!  ========================== ...

  6. 《C++primer》v5 第8章 IO库 读书笔记 习题答案

    8.1.8.2 这一章不咋会啊.. istream &read(istream &is) { int a; auto old_state=is.rdstate(); is.clear( ...

  7. [C++ Primer] 第8章: IO库

    IO类 iostream定义了读写流的基本类型 istream, wistream 从流读取数据 ostream, wostream 向流写入数据 iostream, wiostream 读写流 fs ...

  8. C++ Primer 5th 第1章 开始

    *****代码在Ubuntu g++ 5.31 / clang++ 3.8(C++11)下编写调试***** 每个C++程序必须有一个main( )函数,main( )函数的返回值也必须是int类型, ...

  9. C++ Primer 5th 第12章 动态内存

    练习12.1:在此代码的结尾,b1 和 b2 各包含多少个元素? StrBlob b1; { StrBlob b2 = {"a", "an", "th ...

随机推荐

  1. C51-keil编译常见错误和警告处理53

    keil错误:C51编译器识别错类型有三种:1.致命错误:伪指令控制行有错,访问不存在的原文:2.语法及语义错误:语法和语义错误都发生在原文件:3.警告:警告出现并不影响目标文件的产生,但执行:C_5 ...

  2. HBase Java API入门

    概括 1. 创建.删除及启用禁用表.添加列等都需用到HBaseAdmin,另外需要注意删除,添加列等操作都需要禁用表 2. 表中添加数据,查询等都是和HTable相关,如果是多线程的情况下注意用HTa ...

  3. delphi调用java编写的webservice

    delphi调用java编写的webservice JAVApojo: public class GroupInfo implements Serializable{    private stati ...

  4. Android Studio 遇见的第一个Error

    最近在国内多次尝试在Eclipse下更新SDK无果后,最后终于通过FQ后结束了Google服务器无法访问的噩梦. 顺着墙外的梯子,一并下载Google的Android Studio尝鲜,安装成功后,就 ...

  5. SRM470 - SRM474(1-250pt,500pt)(471-500pt为最短路,474-500pt未做)

    SRM 470 DIV1 250pt 题意:有n个房间排成一排,相邻两个房间之间有一扇关闭着的门(共n-1扇),每个门上都标有‘A’-‘P’的大写字母.给定一个数n,表示第n个房间.有两个人John和 ...

  6. 本地apt

    Ubuntu建立本地源非常實用,很多服務器在局域網沒有網络或者網络很慢的情況下,或者需要批量安裝同样的軟件的時候,如果每一台服務器都去外網下載,是不是很慢,而且也不是一個運維工程師願意這麼幹的!那有什 ...

  7. OBJ-C

    1.直接赋值 NSString *name = @"Starain"; 2.用已经存在的字符串进行初始化 NSString *name2 = [NSString stringWit ...

  8. python:unittest(测试框架)

    一,基本概念 TestCase:所有测试用例的基类 TestLoad:加载测试用例,返回TestSuite(测试套件) TestSuite:创建测试套件 TextTestRunner:运行测试用例 T ...

  9. @Query注解的用法(Spring Data JPA)

    参考文章:http://www.tuicool.com/articles/jQJBNv 1. 一个使用@Query注解的简单例子 @Query(value = "select name,au ...

  10. javascript 属性的特性

    /* 属性的特性: 可以通过调用Object.getOwnPropertyDescriptor()可以获得某个对象特定属性的属性描述符, 从函数名字就可以看出, Object.getOwnProper ...