C++Primer 第8章IO流

写在前面

经过前面初级部分的学习,我进入该书所写的中级阶段——C++标准库,这里会讲到C++所自带的最核心的标准库,之前听过侯捷老师的课,有幸感受到标准库的强大与精妙。正如本书所说,标准库是值得每一位C++程序员应该掌握的。这里给自己鼓劲!
附上几张我上周末终南山的照片:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
是挺美的,不过我去的时候刚好遇上下雨,行走在云中,能见距离不到10m,看不见远处的景吧,但是也别有一番梦境的氛围哈哈哈。
下面开始言归正传!
做个总结吧,我会更改我之前的写博文的风格的,之前的博文风格十分狂野,只有代码没有解释,这对于我的读者,或者我之后的查阅复习都非常的不方便。
那么我将会在之后的文章中,尽可能的解释清楚每一个知识点,这对我也是算是一种新尝试吧,希望我能坚持下去!

首先是main()处理命令行选项,后面会用到

int main(){}

这个东西其实是可以有时也是需要传入实参的,

int main(int argc,char **argv){}
//也可以写成:
int main(int argc,char *argv[]){}

这里传入的第二个参数是一个char数组的指针,所以使用**表示我个人感觉更加通俗一些,这样argv[]里面的数据就可以在主函数块中被调用起来(p196)。值得注意的是该数组最后一个数据应当为0,并且自动生成。

IO类

分为iostream、fstream、sstream
后两者继承于第一个
iostream用于控制流,fstream用于控制文件,sstream用于控制字符串,这三每个又分为读和写两个头文件,为了方便起见,直接include以上三种就可以同时操作读和写了,当然也可以单独的指定。

iostream

这中类对象不能赋值、拷贝和初始化。

a.条件状态

流在执行的时候的状态可以用这些来表示:

//8.1.2
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
	//1
	int ival;
	while (cin >> ival)
	{
		//8.3
		/*
		遇见结束符,与io错误时,返回false
		*/
		cout << ival << endl;
	}
}
//那么如何查询呢我们输入失败在哪里呢
//2
//8.1,8.2
#include<iostream>
#include<stdexcept>
using namespace std;
istream& f(istream& in)
{
	int v;
	while (in >> v, !in.eof())
	{
		//遇见文件结束符,其他的在p280
		if (in.bad())		//badbit置位
			throw runtime_error("IO流错误");
		if (in.fail())		//failbit置位或badbit置位
		{
			cerr << "数据错误,请重试" << endl;
			in.clear();		//复位
			in.ignore(100, '\n');		
			//不管当前之后的100个字符,第101个重新进入循环
			continue;
		}
		cout << v << endl;
	}
}
int main()
{
	//输入一些整数
	f(cin);
}

b.输出缓冲

每个输出流都有一个缓冲流,因为读入计算机需要耗时,那么缓存一起读入则会提高性能。
有以下操作可以使得缓冲区被刷新:

1.缓冲区满
2.换行符endl
3.unitbuf操作符
4.cin出现cout与cerr都会刷新

But,我们需要注意的时程序崩溃时,缓冲区不会被刷新。
但是,p283tie没看懂呀。。。

fstream

fstream继承于iostream因此父类的操作fstream也可以操作

a.fstream 特有的操作

构造函数:fstream fstr(s, mode);	打开s文件,mode为打开模式默认依赖于fstream
fstr.open(s);		打开s并与fstr绑定,这里的s可以时一个字符串哦,也有默认mode
fstr.close();		关闭fstr绑定的文件
str.is_open()		返回bool,检查是否打开成功

b.使用

//8.4
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;

int main()
{
	ifstream in("data");
	if (!in)
		cerr << "无法打开文件" << endl;
	string line;
	vector<string> words;
	while (cin>>line)
		words.push_back(line);
	in.close();
	for (auto i : words)
		cout << i << endl;
}

c.文件模式

这里介绍的也就是mode:

in		读方式
out		写方式
app		写操作定位到文件尾
ate		打开立即定位到文件尾
trunc	截断文件
binary	以二进制j进行IO

//8.5
cin改为getline
//8.8
加入参数app

sstream

这个家伙是处理字符串的,同样继承于iostream

a.特有操作

构造函数:sstream sstr(s)		保存一个string的拷贝
sstr.str()		返回string的拷贝
sstr.str(s)		将s拷贝到sstr中

b.使用

//8.9
#include<iostream>
#include<string>
#include<sstream>
#include<stdexcept>
using namespace std;
istream& f(istream& in)
{
	string str;
	while (in >> str, !in.eof())
	{
		//遇见文件结束符,其他的在p280
		if (in.bad())		//badbit置位
			throw runtime_error("IO流错误");
		if (in.fail())		//failbit置位或badbit置位
		{
			cerr << "数据错误,请重试" << endl;
			in.clear();		//复位
			in.ignore(100, '\n');		
			//不管当前之后的100个字符,第101个重新进入循环
			continue;
		}
		cout << str << endl;
	}
}
int main()
{
	//输入一些字符串
	f(cin);
}
//8.10
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	ifstream in("data");
	if (!in)
		cerr << "无法打开文件" << endl;
	string line;
	vector<string> words;
	while (getline(in, line))
		words.push_back(line);
	in.close();
	for (auto i : words)
		cout << i << endl;
	in.close();
	for (auto it : words)
	{
		istringstream itline(it);
		string word;
		while (itline >> word)
			cout << word << " ";
		cout << endl;
	}
}

这里8.3.2没有做解释,留到第17章吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值