Article Directory
foreword
Long time no see, let's learn the IO stream in C++ together today.
I/O library
These two architecture diagrams are slightly complicated. Here is a clearer IO flow architecture diagram:
In other words, the things we usually use such as cin, cout, cerr, and clog all come from the <iostream> header file, and they are the instance objects of istream and ostream under the
<istream> header file respectively.
The iostream in the <istream> header file inherits the two classes istream and ostream, so the fstream and sstream in the <fstream> and <sstream> classes both have corresponding io functions.
file read and write
Let me explain here by the way: the implementation of friends is authorized by the compiler through a special mechanism so that this function can get the members of the class, not through the this pointer, and istream is actually basic_istream<char, char_traits<char>> an alias.
class Date
{
//友元关系不会被继承
friend istream& operator >>(istream& in, Date& date);
friend ostream& operator <<(ostream& out, const Date& date);
public:
Date(int year = 1, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
}
operator bool()
{
if (_year == 0)
return false;
return true;
}
private:
int _year;
int _month;
int _day;
};
//输入重定向
//
//decltype(cin)& operator >>(decltype(cin)& no,const Date& date)
//basic_istream<char, struct std::char_traits<char>>& operator >>(decltype(cin)& no, const Date& date)
//basic_istream<char>& operator >>(decltype(cin)& no, const Date& date)
istream& operator >>(istream& in, Date& date)
{
//cout << ">> ok" << endl;
//cout << typeid(cin).name() << endl;
in >> date._year >> date._month >> date._day;
return in;
}
//输出重定向
ostream& operator <<(ostream& out, const Date& date)
{
out << date._year << " " << date._month << " " << date._day;
return out;
}
#include <fstream>
struct ServerInfo
{
//string _address;
char _address[32];
int _port;
Date _date;
};
class ConfigManager
{
public:
ConfigManager(string filename) : _filename(filename)
{
}
//二进制读写
void ReadBin(ServerInfo& info)
{
ifstream ifs(_filename, istream::in | istream::binary);
ifs.read((char*)&info, sizeof(info));
}
void WriteBin(const ServerInfo& info)
{
ofstream ofs(_filename, ofstream::out | ofstream::binary);
ofs.write((char*)&info, sizeof(info));
//必然不能写入string,string的内容是new出来的,这里只把指针存好了
}
//文本读写
void ReadText(ServerInfo& info)
{
ifstream ifs(_filename);
ifs >> info._address;
ifs >> info._port;
ifs >> info._date;
}
void WriteText(const ServerInfo& info)
{
ofstream ofs(_filename);
ofs << info._address << endl;
ofs << info._port << endl;
ofs << info._date << endl;
}
private:
string _filename;
};
Serialization and deserialization
// 序列化和反序列化
#include <sstream>
struct ChatInfo
{
string _name; // 名字
int _id; // id
Date _date; // 时间
string _msg; // 聊天信息
};
ostream& operator<<(ostream& out, ChatInfo& info)
{
cout << info._name << info._id << info._date << info._msg << endl;
return out;
}
int main()
{
ChatInfo winfo = {
"丁真", 135246, {
2022, 7, 14 }, "你好我是雪豹" };
stringstream oss;
oss << winfo._name << " ";
oss << winfo._id << " ";
oss << winfo._date << " ";
oss << winfo._msg;
string str = oss.str();
stringstream iss(str);
ChatInfo rinfo;
iss >> rinfo._name;
iss >> rinfo._id;
iss >> rinfo._date;
iss >> rinfo._msg;
cout << rinfo << endl;
return 0;
}
epilogue
This is the whole content of the IO stream, thank you for watching, and see you next time~