【C++从0到王者】第四十三站:IO流

一、C语言的输入与输出

如下所示,是我们常见的C语言输入输出函数

image-20240206174901067

他们分别属于这些流

image-20240206174950907

C语言中我们用到的最频繁的输入输出方式就是scanf ()与printf()。 scanf(): 从标准输入设备(键盘)读取数据,并将值存放在变量中。printf(): 将指定的文字/字符串输出到标准输出设备(屏幕)。注意宽度输出和精度输出控制。C语言借助了相应的缓冲区来进行输入与输出。如下图所示

image-20240206175726995

对输入输出缓冲区的理解:

  1. 可以屏蔽掉低级I/O的实现,低级I/O的实现依赖操作系统本身内核的实现,所以如果能够屏蔽这部分的差异,可以很容易写出可移植的程序。
  2. 可以使用这部分的内容实现“行”读取的行为,对于计算机而言是没有“行”这个概念,有了这部分,就可以定义“行”的概念,然后解析缓冲区的内容,返回一个“行”。

二、流是什么

“流”即是流动的意思,是物质从一处向另一处流动的过程,是对一种有序连续且具有方向性的数据( 其单位可以是bit,byte,packet )的抽象描述。
C++流是指信息从外部输入设备(如键盘)向计算机内部(如内存)输入和从内存向外部输出设备(显示器)输出的过程。这种输入输出的过程被形象的比喻为“流”。
它的特性是:有序连续、具有方向性

为了实现这种流动,C++定义了I/O标准类库,这些每个类都称为流/流类,用以完成某方面的功能

三、C++IO流

C++系统实现了一个庞大的类库,其中ios为基类,其他类都是直接或间接派生自ios类

image-20240206175907045

1.C++标准IO流

C++标准库提供了4个全局流对象cin、cout、cerr、clog,使用cout进行标准输出,即数据从内存流向控制台(显示器)。使用cin进行标准输入即数据通过键盘输入到程序中,同时C++标准库还提供了cerr用来进行标准错误的输出,以及clog进行日志的输出,从上图可以看出,cout、cerr、clog是ostream类的三个不同的对象,因此这三个对象现在基本没有区别,只是应用场景不同

在使用时候必须要包含文件并引入std标准命名空间

C++使用cin,和cout有如下好处

image-20240206175328578

注意:

  1. cin为缓冲流。键盘输入的数据保存在缓冲区中,当要提取时,是从缓冲区中拿。如果一次输入过多,会留在那儿慢慢用,如果输入错了,必须在回车之前修改,如果回车键按下就无法挽回了。只有把输入缓冲区中的数据取完后,才要求输入新的数据
  2. 输入的数据类型必须与要提取的数据类型一致,否则出错。出错只是在流的状态字state中对应位置位(置1),程序继续
  3. 空格和回车都可以作为数据之间的分格符,所以多个数据可以在一行输入,也可以分行输入。但如果是字符型和字符串,则空格(ASCII码为32)无法用cin输入,字符串中也不能有空格。回车符也无法读入
  4. cin和cout可以直接输入和输出内置类型数据,原因:标准库已经将所有内置类型的输入和输出全部重载了
  5. 对于自定义类型,如果要支持cin和cout的标准输入输出,需要对<<和>>进行重载。
  6. 在线OJ中的输入和输出 (见下面)
  7. istream类型对象转换为逻辑条件判断值(见下面)

关于注意第六点和第七点中的在线OJ的输入和输出

  • 对于IO类型的算法,一般都需要循环输入:
  • 输出:严格按照题目的要求进行,多一个少一个空格都不行。
  • 连续输入时,vs系列编译器下在输入ctrl+Z时结束

我们经常可以看到下面的代码

int main()
{
	string str;
	while (cin >> str) //operator>>(cin, str)
	{
		cout << str << endl;
	}

	return 0;
}

这里输入其实就是利用cin的返回值进行判断的,调用的是operator>>(cin,str)这个函数进行实现的

image-20240206181710928

这个返回的istream对象其实就是cin,也就是在用cin进行判断,cin这个对象理论上来说是不可以作为while的判断条件的。但是可以operator bool进行重载来实现(C++98重载的是operator void*() const

image-20240206182236838


我们看下面的代码

在下面的代码中,我们原本只支持内置类型转化为自定义类型。但是如果我们加上了operator重载类型的话,就支持自定义类型转化为内置类型了。这里还不需要写返回值,因为返回值其实已经有了,就是重载的类型。

class A
{
public:
	A(int a)
		:_a(a)
	{}
	//不需要写返回值
	operator int()
	{
		return _a;
	}
	operator bool()
	{
		return _a;
	}
	int _a;
};
int main()
{
	//内置类型转化为自定义类型
	A a1 = 100;
	//自定义类型转化为内置类型
	int i = a1;
	cout << i << endl;

	bool ret = a1;
	cout << ret << endl;
	return 0;
}

运行结果为

image-20240206184044891

如果我们加上了explicit关键字呢?

operator int 这种场景中,explicit 关键字同样可以用来禁止隐式类型转换。如果一个类有一个 operator int 成员函数,并且被声明为 explicit,那么该类的对象就不能隐式转换为 int 类型,而是需要显式调用 operator int。这样可以避免意外的类型转换,增加代码的可读性和安全性。

如下所示,我们发现无法进行转化了

image-20240206184906746

所以我们就必须得显示转化

operator int() 没有使用 explicit 关键字时,会发生隐式类型转换,可能导致意外的行为,例如:

#include <iostream>

class MyClass {
public:
MyClass(int val) : m_val(val) {}
operator int() const { return m_val; }

private:
int m_val;
};

void printInt(int num) {
std::cout << "Integer: " << num << std::endl;
}

int main() {
MyClass obj(5);
printInt(obj); // 隐式类型转换,使用 operator int() 将 MyClass 转换为 int
return 0;
}
cpp复制代码

在上面的例子中,MyClass 类有一个 operator int(),允许将 MyClass 对象隐式地转换为 int。然而,printInt() 函数预期接收一个 int 类型参数,但实际上传入了一个 MyClass 对象,这可能导致意外的行为和错误。

使用 explicit 关键字修饰 operator int() 后,将禁止隐式类型转换,只能以显式方式进行调用,例如:

#include <iostream>

class MyClass {
public:
MyClass(int val) : m_val(val) {}
explicit operator int() const { return m_val; } // 添加 explicit 关键字

private:
int m_val;
};

void printInt(int num) {
std::cout << "Integer: " << num << std::endl;
}

int main() {
MyClass obj(5);
printInt(obj); // 编译错误,不能隐式地将 MyClass 转换为 int
printInt(static_cast<int>(obj)); // 显式地进行转换
return 0;
}
cpp复制代码

在这个修复后的例子中,尝试隐式地将 MyClass 对象传递给 printInt() 函数会导致编译错误,因为禁止了隐式类型转换。必须显式地使用 static_cast<int>(obj)MyClass 转换为 int。这样可以避免意外的行为,增加代码的安全性和可读性。

我们再来看前面的例子

class A
{
public:
	A(int a)
		:_a(a)
	{}
	//不需要写返回值
	explicit operator int()
	{
		return _a;
	}
	explicit operator bool()
	{
		return _a;
	}
	int _a;
};
int main()
{
	//内置类型转化为自定义类型
	A a1 = 100;
	//自定义类型转化为内置类型
	//int i0 = a1;  //隐式的,是错的
	int i1 = (int)a1; //显式的,是正确的
	int i2 = static_cast<int>(a1); //显式的,是正确的
	cout << i1 << " " << i2 << endl;

	bool ret = (bool)a1;
	cout << ret << endl;


	double d0 = (int)a1; //结果为100
	double d1 = (bool)a1; //结果为1
	cout << d0 << " " << d1 << endl;

	return 0;
}

运行结果为

image-20240206185837967


再回过头来看前面的。这时候我们就理解了这个代码了。本质就是cin的返回值在不断的调用operator bool

image-20240206191014048

operator bool这个函数返回一个布尔值,用于判断是否设置了错误标志(即 failbit 或 badbit)。请注意,该函数的返回值与成员函数 good 不同,而是与成员函数 fail 的相反值。


我们可以看下面的代码

这里的cin >> a >> b就相当于cin.operator(cin, a).operator(cin, b).operator bool();

int main()
{
	int a, b;
	while (cin >> a >> b) // cin.operator(cin, a).operator(cin, b).operator bool();
	{
		cout << a << endl;
		cout << b << endl;
	}

	return 0;
}

2.C++文件IO流

2.1 C语言的方式

如下代码是我们使用C语言的方式将一个数据写到内存当中

class Date
{
	friend ostream& operator << (ostream& out, const Date& d);
	friend istream& operator >> (istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	operator bool()
	{
		// 这里是随意写的,假设输入_year为0,则结束
		if (_year == 0)
			return false;
		else
			return true;
	}
private:
	int _year;
	int _month;
	int _day;
};
istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
ostream& operator << (ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day;
	return out;
}
int main()
{
	Date d(2024, 2, 6);
	FILE* fin = fopen("text.txt", "w");
	fwrite(&d, sizeof(Date), 1, fin);
	fclose(fin);
	return 0;
}

我们最后写入的东西是这样的

image-20240206194345572

这是因为fwrite的的写入的方式是以二进制的形式写入的,内存当中是什么样子就写入什么样子。

我们也可以这样做,直接就以文本的方式写入

class Date
{
	friend ostream& operator << (ostream& out, const Date& d);
	friend istream& operator >> (istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	operator bool()
	{
		// 这里是随意写的,假设输入_year为0,则结束
		if (_year == 0)
			return false;
		else
			return true;
	}
	operator string()
	{
		string str;
		str += to_string(_year);
		str += " ";
		str += to_string(_month);
		str += " ";
		str += to_string(_day);
		return str;
	}
private:
	int _year;
	int _month;
	int _day;
};
istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
ostream& operator << (ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day;
	return out;
}
int main()
{
	Date d(2024, 2, 6);
	FILE* fin = fopen("text.txt", "w");
	//fwrite(&d, sizeof(Date), 1, fin);
	//fclose(fin);
	string str = d;
	fputs(str.c_str(), fin);

	return 0;
}

运行结果为

image-20240206194833191

这样我们就可以看懂了

2.2 C++的方式

这里需要注意,istream提取,是读!,ostream是插入,才是写

C++根据文件内容的数据格式分为二进制文件和文本文件。采用文件流对象操作文件的一般步骤:

  1. 定义一个文件流对象
  • ifstream ifile(只输入用)
  • ofstream ofile(只输出用)
  • fstream iofile(既输入又输出用)
  1. 使用文件流对象的成员函数打开一个磁盘文件,使得文件流对象和磁盘文件之间建立联系
  2. 使用提取和插入运算符对文件进行读写操作,或使用成员函数进行读写
  3. 关闭文件

对于ofstream,它的打开函数为

image-20240206200011337

对于第二个参数,有如下选项,我们可以认为它就是一些的枚举值,我们可以将它们给或起来。由于它默认有缺省值,所以其实我们可以不用搞这个参数,但是如果我们需要用二进制的方式的话,就需要这些了

image-20240206200105858

不过其实更多的是直接在构造函数就打开了,我们可以看到,与open是一样的

image-20240206200213886

对于这个文件的关闭,close,我们一般可以不用去写它,因为有析构函数

int main()
{
	ofstream ofs("file.txt", ios_base::out | ios_base::binary);
	return 0;
}

这里我们由于要用二进制写,就要用write接口

image-20240206201333745

如下代码所示,就是二进制的方式写入

#include <fstream>

class Date
{
	friend ostream& operator << (ostream& out, const Date& d);
	friend istream& operator >> (istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	operator bool()
	{
		// 这里是随意写的,假设输入_year为0,则结束
		if (_year == 0)
			return false;
		else
			return true;
	}
	operator string()
	{
		string str;
		str += to_string(_year);
		str += " ";
		str += to_string(_month);
		str += " ";
		str += to_string(_day);
		return str;
	}
private:
	int _year;
	int _month;
	int _day;
};
istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
ostream& operator << (ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day;
	return out;
}
int main()
{
	Date d(2024, 2, 6);
    //二进制方式写入
	ofstream ofs("file.txt", ios_base::out | ios_base::binary);
	ofs.write((const char*)&d, sizeof(d));
	return 0;
}

最终文本的内容为

image-20240206201831063

如果我们要按照文本的方式,我们可以这样写

istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
ostream& operator << (ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day;
	return out;
}
int main()
{
	Date d(2024, 2, 6);
    //文本方式写入
	ofstream ofs("file.txt");
	ofs << d;
	return 0;
}

最终内容为

image-20240206202005123

这里调用的这个就是流插入的重载。

注意下面的,ofstream是ostream的派生类,这里用的继承的一个特性

image-20240206202226124

我们再试下下面的代码

class Date
{
	friend ostream& operator << (ostream& out, const Date& d);
	friend istream& operator >> (istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	operator bool()
	{
		// 这里是随意写的,假设输入_year为0,则结束
		if (_year == 0)
			return false;
		else
			return true;
	}
	operator string()
	{
		string str;
		str += to_string(_year);
		str += " ";
		str += to_string(_month);
		str += " ";
		str += to_string(_day);
		return str;
	}
private:
	int _year;
	int _month;
	int _day;
};
istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
ostream& operator << (ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day;
	return out;
}
struct ServerInfo
{
	char _address[32];
	int _port;
	Date _date;
};
struct ConfigManager
{
public:
	ConfigManager(const char* filename = "file.txt")
		:_filename(filename)
	{}
	void WriteBin(const ServerInfo& info)
	{
		ofstream ofs(_filename, ios_base::out | ios_base::binary);
		ofs.write((const char*)&info, sizeof(info));
	}
	void ReadBin(ServerInfo& info)
	{
		ifstream ifs(_filename, ios_base::in | ios_base::binary);
		ifs.read((char*)&info, sizeof(info));
	}
	// C++文件流的优势就是可以对内置类型和自定义类型,都使用
	// 一样的方式,去流插入和流提取数据
	// 当然这里自定义类型Date需要重载>> 和 <<
	// istream& operator >> (istream& in, Date& d)
	// ostream& operator << (ostream& out, const Date& d)
	void WriteText(const ServerInfo& info)
	{
		ofstream ofs(_filename);
		ofs << info._address << " " << info._port << " " << info._date;
	}
	void ReadText(ServerInfo& info)
	{
		ifstream ifs(_filename);
		ifs >> info._address >> info._port >> info._date;
	}
private:
	string _filename; // 配置文件
};
int main()
{
	ServerInfo winfo = { "192.0.0.1", 80, { 2022, 4, 10 } };
	// 二进制读写---简单高效,缺点是写在文件的内容看不懂
	ConfigManager cf_bin("test.bin");
	cf_bin.WriteBin(winfo);
	ServerInfo rbinfo;
	cf_bin.ReadBin(rbinfo);
	cout << rbinfo._address << " " << rbinfo._port << " " << rbinfo._date << endl;
	// 文本读写
	ConfigManager cf_text("test.text");
	cf_text.WriteText(winfo);
	ServerInfo rtinfo;
	cf_text.ReadText(rtinfo);
	cout << rtinfo._address << " " << rtinfo._port << " " << rtinfo._date << endl;
	return 0;
}

如下是运行结果

image-20240206205511696


需要注意到是,在这个数据结构中,这个char数组不要换成string,否则二进制读取的时候可能存在一些问题。我们这里没问题的原因是,vs对于小的string是会放到一个char数组中的。如果是比较大的字符串,那么程序会直接崩溃

struct ServerInfo
{
	char _address[32];
	int _port;
	Date _date;
};

因为字符串较大的时候,这个string的字符串在堆上存储,里面仅仅存储一个地址。我们在使用下一个进程在进行去读取的时候,这个地址已经不认识了。就是野指针了。所以二进制读写不能用string,否则写出去就是一个指针。


对于文本读写,需要注意的是,写入的时候,要加上空格或者换行标志。

因为流提取运算符并不会读取空格或者换行。如果我们不加空格,那么会出现一些问题。


如果我们要读取文件,还可以用get接口,它可以获取字符

image-20240206231907266

int main()
{
	ifstream ifs("test.cpp");
	char ch;
	while (ifs.get(ch))
	{
		cout << ch;
	}
	return 0;
}

运行结果为。可以看到我们的文件内容全部被打印出来了

image-20240206232047455

它判断读取结束的标志也是通过operator bool来进行的!一旦读取结束,就设置标志位。

四、stringstream

在C语言中,如果我们想要将一个整数转化为字符串是这样做的

int main()
{
	int x;
	cin >> x;
	char buffer[128];
	sprintf(buffer, "int:%d", x);
	cout << buffer << endl;
	return 0;
}

image-20240206232730880

当然也有可能是使用itoa函数

但是两个函数在转化时,都得需要先给出保存结果的空间,那空间要给多大呢,就不太好界定,而且转化格式不匹配时,可能还会得到错误的结果甚至程序崩溃。

在C++中,为了更好的支持自定义类型,stringstream类来解决问题

在程序中如果想要使用stringstream,必须要包含头文件#include <sstream>,在该头文件下,标准库三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作.

比如下面我们使用ostringstream来写入一个数据。这里的操作核心就是利用日期类的的流插入重载完成的

class Date
{
	friend ostream& operator << (ostream& out, const Date& d);
	friend istream& operator >> (istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	operator bool()
	{
		// 这里是随意写的,假设输入_year为0,则结束
		if (_year == 0)
			return false;
		else
			return true;
	}
	operator string()
	{
		string str;
		str += to_string(_year);
		str += " ";
		str += to_string(_month);
		str += " ";
		str += to_string(_day);
		return str;
	}
private:
	int _year;
	int _month;
	int _day;
};
istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
ostream& operator << (ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day;
	return out;
}
int main()
{
	Date d(2024, 2, 7);
	ostringstream oss;
	oss << d;
	string str = oss.str();
	cout << str << endl;
	return 0;
}

image-20240207001032495

那么我们如何解析这个,即如何提取出这个字符串使之变为Date对象呢?

我们可以用istringstream

image-20240207002744559

int main()
{
	Date d(2024, 2, 7);
	ostringstream oss;
	oss << d;
	string str = oss.str();
	//cout << str << endl;
	istringstream iss(str);
	Date d2;
	iss >> d2;
	cout << d2;
	return 0;
}

运行结果为

image-20240207002943924

这里的ostringstream和istringstream其实就是序列化和反序列化

序列化就是要进行简单的描述,反序列化就是解析出来


下面是一个简单的使用stringstream进行序列化和反序列化的一个例子

struct ChatInfo
{
	string _name; // 名字
	int _id; // id
	Date _date; // 时间
	string _msg; // 聊天信息
};

int main()
{
	//序列化
	ChatInfo winfo = { "张三", 151546546, { 2022, 4, 10 }, "晚上一起看电影吧" };
	stringstream ss;
	ss << winfo._name << " " << winfo._id << " " << winfo._date << " " << winfo._msg;
	//str就是序列化后的字符串
	string str = ss.str();
	//解析字符串
	ChatInfo rInfo;
	istringstream is(str);
	is >> rInfo._name >> rInfo._id >> rInfo._date >> rInfo._msg;
	//打印出我们提取的字符串
	cout << "-------------------------------------------------------" << endl;
	cout << "姓名:" << rInfo._name << "(" << rInfo._id << ") ";
	cout << rInfo._date << endl;
	cout << rInfo._name << ":>" << rInfo._msg << endl;
	cout << "-------------------------------------------------------" << endl;
	return 0;
}

运行结果为

image-20240207004902353

如果我们要清空stringstream对象的字符串内容,有下面的方式

stringstream ss;
ss.str(""); //清空字符串,括号内可以写入任何你想替换的字符串,如果只是想清空则写入空字符串即可。
ss.clear(); //清空状态位,如错误标志位,这并不会清除该对象中存储的数据
  • 16
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青色_忘川

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值