IO流
linux下叫终端,VS下叫控制台 一张继承图,cin是istream的对象,cout是ostream的对象 cin>> ->cin.operator>>() cin比scanf慢的原因是底层封装太多,做的太笼杂了,跟编译没有关系 cin是如何做到连续的输入多个数?多个函数调用,返回值有关 真正的优势是,cin,cout可以重载自己想要的类型
1 .operator bool()
OJ多种输入,while(cin>>str){} C语言while(scanf(“%s”,a)!=EOF){} 类型运算符重载 重载了operator bool() 这个函数不需要返回值,意思就是这玩意可以转成bool
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
using namespace std;
class B
{
public:
operator bool()
{
return _a;
}
int _a;
};
int main()
{
B b;
while(b)
{
cin>>b._a;
}
return 0;
}
2.C语言 将结构的数据写到文件当中去
二进制读写 fwrite,fread 文本读写 fputs/fscanf/fprintf 字符串的话可以直接写,如果是其他,先要把它转成字符串再写出去,回来再读进来 printf和fprintf的区别是什么?fprintf是写到某一个文件中去 点输出旁边的test.cpp右击可以跳到文件所在的目录 fscanf为什么读不出来?因为它是以空格或者换行为区分的
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<assert.h>
using namespace std;
struct ServerInfo
{
char _ip[32];
int _port;
};
void C_write_test()
{
ServerInfo info = { "127.0.0.1",80 };
FILE* fout = fopen("test.h", "w");
assert(fout);
fprintf(fout, "%s %d", info._ip, info._port);
fclose(fout);
printf("%s %d", info._ip, info._port);
}
void C_read_test()
{
FILE* fin = fopen("test.txt", "r");
assert(fin);
ServerInfo info ;
fscanf(fin,"%s%d", info._ip, &info._port);
fclose(fin);
printf("%s:%d", info._ip, info._port);
}
int main()
{
C_write_test();
C_read_test();
return 0;
}
3 . C++的写入和写出
定义一个Configmanager类 包string 和 fstream(文件流) 两个头文件 ofstream是写 w,a,r 是文本文件,如果想要二进制文件,后面要加一个b 实现二进制读写和普通读写 读一个Date类 重载流插入
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<sstream>
using namespace std;
class Date
{
public:
friend istream& operator>>(istream& in, Date& d);
friend ostream& operator<<(ostream& out, const Date& d);
Date(int year, int month, int date)
:_year(year)
,_month(month)
,_date(date)
{
}
private:
int _year;
int _month;
int _date;
};
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._date;
return in;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << " " << d._month << " " << d._date;
return out;
}
struct ServerInfo
{
char _ip[32];
int _port;
Date _date;
};
class 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::binary);
ifs.read((char*)&info,sizeof(info));
}
void WriteText(const ServerInfo& info)
{
ofstream ofs(_filename);
ofs << info._ip << " " << info._port<<" "<<info._date;
}
void ReadText(ServerInfo& info)
{
ifstream ifs(_filename);
ifs >> info._ip>> info._port>>info._date;
}
private:
string _filename;
};
int main()
{
ServerInfo winfo = { "192.0.0.1", 80 ,{2022,5,3} };
ConfigManager cf_txt("test.txt");
cf_txt.WriteBin(winfo);
ServerInfo rtinfo = { "",0 ,{0,0,0} };
cf_txt.ReadText(rtinfo);
cout << rtinfo._ip << " " << rtinfo._port << endl;
return 0;
}
4 .字符串流stringstream
调中间的str函数可以把里面的string取出来 应用:聊天时候的通讯,发出去的时候是不是都是字符串 ostringstream发,istringstream解析收 -> 专业名词:序列化和反序列化
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<sstream>
#include<fstream>
#include<string>
using namespace std;
class date
{
public:
friend istream& operator>>(istream& in, date& d);
friend ostream& operator<<(ostream& out, const date& d);
date(int year, int month, int date)
:_year(year)
, _month(month)
, _date(date)
{
}
private:
int _year;
int _month;
int _date;
};
istream& operator>>(istream& in, date& d)
{
in >> d._year >> d._month >> d._date;
return in;
}
ostream& operator<<(ostream& out, const date& d)
{
out << d._year << " " << d._month << " " << d._date;
return out;
}
struct Info
{
string _name;
int _id;
date _date;
string _msg;
};
int main()
{
Info winfo = { "person",1001,{2002,5,3},"晚上一起看电影把" };
ostringstream oss;
oss << winfo._name << winfo._id << winfo._date << winfo._msg;
string str = oss.str();
cout << str << endl;
Info rInfo = { "",0,{0,0,0},"" };
istringstream iss(str);
iss >> rInfo._name >> rInfo._id >> rInfo._date >> rInfo._msg;
cout << "姓名:" << rInfo._name << " (" << rInfo._id << ") ";
cout << rInfo._date << endl;
cout << rInfo._name << ":>" << rInfo._msg << endl;
return 0;
}
|