C++IO流

前言

这一讲讲io流
在这里插入图片描述

1. cout,cin

1.1 cout,cerr,clog

cout << "11111" << endl;
cerr << "11111" << endl;
clog << "11111" << endl;

在这里插入图片描述

1.2 四个状态

在这里插入图片描述
io流总共有四个状态,good表示这个可以正常读取,eof表示读取到了文件末尾
fail读取异常结束,bad就是io流本身出现了问题,这个我们一般遇不到

string s;
while (cin >> s)
{
	cout << s;
}

这个程序要结束就是输入ctrl+z,加回车
这个的作用就是设置fail
而退出while循环还可以ctrl+c,这个比较暴力,就是直接结束程序,不运行后面的了,
ctrl+z还可以运行后面的

cout << cin.good() << endl;
cout << cin.eof() << endl;
cout << cin.fail() << endl;
cout << cin.bad() << endl;

string s;
while (cin >> s)
{
	cout << cin.good() << endl;
	cout << cin.eof() << endl;
	cout << cin.fail() << endl;
	cout << cin.bad() << endl;
	cout << s;
}

cout << cin.good() << endl;
cout << cin.eof() << endl;
cout << cin.fail() << endl;
cout << cin.bad() << endl;

调用对应函数,我们就可以查看IO流的状态
在这里插入图片描述
看得出来,IO流要正常运行,就必须要good状态为1,其余为0
Ctrl+z就是将fail设为1,eof设为1,good设为0,所以就不行了

string s;
while (cin >> s)
{
	cout << cin.good() << endl;
	cout << cin.eof() << endl;
	cout << cin.fail() << endl;
	cout << cin.bad() << endl<<endl;
	cout << s<<endl;
}

cout << cin.good() << endl;
cout << cin.eof() << endl;
cout << cin.fail() << endl;
cout << cin.bad() << endl;

string s1;
while (cin >> s1)
{
	cout << s1<<endl;
}

在这里插入图片描述
而且good状态一旦为0,就无法在继续正常使用IO流了,除非更改状态

在这里插入图片描述
这些函数都是可以更改状态的,但不如clear,clear就是将状态设置为最开始的状态的

cout << "11111" << endl;
cerr << "11111" << endl;
clog << "11111" << endl;

cout << cin.good() << endl;
cout << cin.eof() << endl;
cout << cin.fail() << endl;
cout << cin.bad() << endl;

string s;
while (cin >> s)
{
	cout << cin.good() << endl;
	cout << cin.eof() << endl;
	cout << cin.fail() << endl;
	cout << cin.bad() << endl<<endl;
	cout << s<<endl;
}

cin.clear();

cout << cin.good() << endl;
cout << cin.eof() << endl;
cout << cin.fail() << endl;
cout << cin.bad() << endl;

string s1;
while (cin >> s1)
{
	cout << s1<<endl;
}

在这里插入图片描述

1.3 缓冲区

int j = 0;
cin >> j;
cout << cin.good() << endl;
cout << cin.eof() << endl;
cout << cin.fail() << endl;
cout << cin.bad() << endl;
cout << j<<endl;
int i;
cin >> i;
cout << i<<endl;

在这里插入图片描述
当输入123s的时候,就相当于向缓冲区中输入了123s,当用整型j读取的时候,只会读取j,当读取i的时候,就不会让我们输入了,因为缓冲区中还有东西,但是无法读取这个字母,所以缓冲区中还有一个s字母,但是cin的时候都会把这个数据设置为0,所以i为0,还有就是没有读取到是不会影响状态的,但是读取失败就会影响状态了

int j = 0;
cin >> j;
cout << cin.good() << endl;
cout << cin.eof() << endl;
cout << cin.fail() << endl;
cout << cin.bad() << endl;
cout << j<<endl;
int i;
cin >> i;
cout << i<<endl;
cout << cin.good() << endl;
cout << cin.eof() << endl;
cout << cin.fail() << endl;
cout << cin.bad() << endl;

在这里插入图片描述
因为wer对应不上整型,所以状态变为fail,这都是缓冲区的问题

	char c = 0;
	while (cin.get(c))
	{
		///
	}

我们可以这样,把缓冲区里的字符读入c中,这样缓冲区就干净了,然后ctrl+z,后面再clear

	// 在io需求比较高的地方,如部分大量输入的竞赛题中,加上以下3行代码
// 可以提高C++IO效率
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);

下面说明第一行,首先C语言和C++都有各自的缓冲区
如果这样
printf(“a”);
cout<<‘b’;
printf(“c\n”);
ac存入C语言的缓冲区,b存入C++的缓冲区
但是,因为换行具有刷新缓冲区的作用,所以正常来说,会先打印ac,再打印b,但是系统会处理这个问题,处理就会降低效率,第一行就是取消这个处理
第二行是因为,C++中,cin的缓冲区和cout的也有关联,cin的刷新,也会带动cout的刷新,所以会降低效率,这两行就是取消这个特性

反正记住可以提高效率就可以了

2. 文件流

在这里插入图片描述
直接讲使用
要有头文件fstream

ofstream os;
os.open("test.txt");

在C++中IO流都是类
这个是写入文件,没有文件就创造文件

ofstream os("test.txt");

还可以这样写
向文件中写入数据的时候,除了调用特定的函数外,还可以直接用<<重载

os << "asdfgghj"<<endl;
os << 1<<endl;

在这里插入图片描述
要关闭文件,也不用调用close,因为类有析构函数

ifstream is("test.txt");
string s;
int i;
char c;
while (is >> c)
{
	cout << c;
}

这个就可以打印出文件中的所有内容

ifstream is("test.txt");
string s;
int i;
is >> s >> i;
cout << s << endl;
cout << i << endl;

这个就可以得到文件中的内容存到数据中,怎么写入的就怎么读取

ofstream os("test.txt");
Date d(2024, 9, 15);
cout << d;
os << d;

在这里插入图片描述
在这里插入图片描述
对于自定义类型的文件存贮,这个自定义类型cout重载怎么打印的,就怎么存储在文件中

ifstream is("test.txt");
Date d;
is >> d;
cout << d;

在这里插入图片描述
当然怎么存的就怎么读取,肯定是不会出错的

//string s("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//ofstream os("test.txt");
//os << s;

string s;
ifstream is("test.txt");
is >> s;
cout << s;

在这里插入图片描述
对于动态开辟的也是这样的

string s("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
ofstream os("test.txt",ios_base::out|ios_base::binary);
os << s;

如果要二进制写入的话,就要在后面加上这个东西,因为默认是ios_base::out写入,就不是二进制写入,加上|ios_base::binary就可以二进制写入了,二进制写入的,我们看到就是乱码
在这里插入图片描述
在这里插入图片描述
当然全是字符a写入,我们看到的还是字符a,换一下就可以了

string s("aaaaaaaaa好吃得混吃等死刺激我aaaaaaaaa");
ofstream os("test.txt",ios_base::out|ios_base::binary);
os << s;

在这里插入图片描述

string s;
ifstream is("test.txt", ios_base::in | ios_base::binary);
is >> s;
cout << s;

在这里插入图片描述
这个就是二进制读

3. 字符串流

这个流就也是很简单了,和前面的很类似
头文件sstream
在这里插入图片描述

ostringstream oss;
oss << "123456" << "asdfg" << 1 << 233;
cout << oss.str();

在这里插入图片描述

这个流,就是把内容存在字符串中,字符串就在类中
oss中存在一个string类型的成员变量,所以可以存储
str()就可以访问到这个字符串了

ostringstream oss("111qqdddaaaaaa");
//oss << "123456" << "asdfg" << 1 << 233;
cout << oss.str()<<endl;
oss << "123456" << "asdfg" << 1 << 233;
cout << oss.str()<<endl;
oss << "ssssssssssssssssssss";
cout << oss.str() << endl;

当然还可以构造函数那里存,只不过后来会覆盖

ostringstream oss;
oss << "123456" << "asdfg" << 1 << 233;
cout << oss.str() << endl;

istringstream iss(oss.str());
string s;
iss >>s;
cout << s;

ostringstream 是把数据放入字符串中,那么istringstream 就是将数据从字符串中取出了
在这里插入图片描述

总结

这就是IO流的博客了,因为在上学,所以博客以后可能都会写的比较少

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值