本节主要看代码理解
I/O流继承关系

 iostream
 
 主要类 cin cout cerr clog
  
while(cin>>str)
{
//处理
}
 当接收ctrl z 或 ctrl c时,会停止, 原理:重载操作符bool,令指定istream为false
清理cin缓存
		if (cin.fail())
		{
			cin.clear();
			char ch;
			while (cin.get(ch))
			{
				//cout << ch;
    		}
		}cin.clear
 ctrl z后cin都不可用,因为cin前会检查goodbit
 cin.clear() 可恢复cin,使其可用
 ctrl c 直接杀进程
 四种文件状态,用一个整形表示,不同二进制位表不同状态
 eofbit
 failbit
 badbit
 goodbit
 向int 中cin整形加字符(22s)
 int会在第一个非整型前停下,剩下的加到缓存区,留给下个cin
 cin.get(ch)
 
 将cin缓存区第一个字符给ch并删除,类似pop,可用于清空缓存(结合clear)
  
 I/O提速
 
解决cin,cout效率比printf/scanf低下
 加上
 ios_base::sync_with_stdio(false) //关闭C/C++同步
 cin.tie(nullptr)//解除绑定关系
 cout.tie(nullptr)//解除绑定关系
  
 fstream
 
 向文件写入/出数据
 1.直接写二进制
 
struct ServerInfo
{
	//char _address[32];
	string _address;
	int _port;
	Date _date;
};
struct ConfigManager
{
public:
	ConfigManager(const char* filename)
		:_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));
	}
	// 21:15继续
	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.1111111111111111111111", 80, { 2022, 4, 10 } };
//
//	// 二进制读写
//	ConfigManager cf_bin("test.bin");
//	cf_bin.WriteBin(winfo);
//
//	return 0;
//}
//int main()
//{
//	// 二进制读写
//	ConfigManager cf_bin("test.bin");
//
//	ServerInfo rbinfo;
//	cf_bin.ReadBin(rbinfo);
//
//	cout << rbinfo._address << " " << rbinfo._port << " " << rbinfo._date << endl;
//
//	return 0;
//}
 写string等有指针的会有大坑
 2.都转成字符串(重载流插入流提取)
 
 
 
ofstream
int main()
{
	ofstream ofs("test.txt");
	//ofs.open("test.txt");
	ofs.put('x');
	ofs.write("hello\n", 6);
	ofs << "22222222" << endl;
	int x = 111;
	double y = 1.11;
	ofs << x << endl;
	ofs << y << endl;
	//ofs.close();
	return 0;
}ifstream
int main()
{
	//ifstream ofs("test.cpp");
	ifstream ofs("D:\\360MoveData\\Users\\xjh\\Desktop\\8-10.png", ios_base::in | ios_base::binary);
	int n = 0;
	while (!ofs.eof())
	{
		char ch = ofs.get();
		//cout << ch;
		++n;
	}
	cout << n << endl;
	return 0;
}
 stringstream
 
 将其看成控制台我们与程序交互即可, 我们输入的是都字符串,但程序会根据接收值类型自动转为整形/浮点数, 还会对多参数对象自动调用构造函数(不是重载)
#include<sstream>
//int main()
//{
//	int i = 123;
//	Date d = { 2022, 4, 10 };
//	ostringstream oss;
//	oss << i << endl;
//	oss << d << endl;
//
//	string s = oss.str();
//	cout << s << endl;
//
//	//istringstream iss(s);
//	//istringstream iss;
//	//iss.str("100 2024 9 9");
//	istringstream iss("100 2024 9 9");
//	int j;
//	Date x;
//	iss >> j >> x;
//
//	return 0;
//}
// json库
struct ChatInfo
{
	string _name; // 名字
	int _id;      // id
	Date _date;   // 时间
	string _msg;  // 聊天信息
};
int main()
{
	// 结构信息序列化为字符串
	ChatInfo winfo = { "张三", 135246, { 2022, 4, 10 }, "晚上一起看电影吧" };
	ostringstream oss;
	oss << winfo._name << " " << winfo._id << " " << winfo._date << " " << winfo._msg;
	string str = oss.str();
	cout << str << endl << endl;
	// send(str);
	// str = recv()
	ChatInfo rInfo;
	istringstream iss(str);
	iss >> 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;
}









![[001-03-007].第26节:分布式锁迭代3->优化基于setnx命令实现的分布式锁-防锁的误删](https://i-blog.csdnimg.cn/direct/09e4fa950d96422e91f96cd371d5f474.png)








