栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

IO流(C语言,C++)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IO流(C语言,C++)

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
#include
#include
#include

using namespace std;

class B
{
public:
	//不用带任何参数
	operator bool()
	{
		return _a;
	}
	int _a;
};

int main()
{
	//int i = 1;
	//double d = 2.2;
	//cout << i << endl;//cout.operator<<(i);
	//cout << d << endl;//cout.operator<<(d);
	//cout << i << endl << d << endl;
	printf可以输出多个输出
	//printf("%d %lf", i, d);

	//cin >> i;
	//cin >> d;
	//cout << i << d;
	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
#include
#include
#include

using namespace std;

struct ServerInfo
{
	char _ip[32];//ip
	int _port;	//端口
};

void C_write_test()
{
	ServerInfo info = { "127.0.0.1",80 };
	//fopen/fclose
	//二进制读写 fwrite/fread 
	//文本读写 fprintf/fscanf
	FILE* fout = fopen("test.h", "w");
	assert(fout);
	//fwrite(&info, sizeof(info), 1, fout);
	fprintf(fout, "%s %d", info._ip, info._port);
	fclose(fout);
	printf("%s %d", info._ip, info._port);
}

void C_read_test()
{
	//fopen/fclose
	//二进制读写 fwrite/fread 
	//文本读写 fprintf/fscanf
	FILE* fin = fopen("test.txt", "r");
	assert(fin);
	ServerInfo info ;
	//fread(&info, sizeof(info), 1, fin);
	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
#include
#include
#include
#include

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<<" "<
		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
#include
#include
#include
#include
#include
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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853509.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号