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

15.C++的I/O流

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

15.C++的I/O流

目录

1. iostream(I/O流)

1.1多组输入问题

2. fstream (文件流)

3.stringstream(字符串流)


1. iostream(I/O流)

需要注意:输入数据使用enter或空格进行分隔数据,scanf fscanf 等等。

1.1多组输入问题

在C语言中的多组测试用例输入:

int main()
{
	char a[100];
	while (scanf("%s",a)!=EOF)
	{

	}

	return 0;
}

这段程序如何终止呢?需要ctrl+z(给一个结束信号)

对于cin有多组输入:也是ctrl+z结束。

#include
#include
using namespace std;
int main()
{
	string str;
	while (cin>>str)//这里是函数的调用operator(cin,str),它的返回值是istream&
	{
	}

	return 0;
}

cin>>str,这里是函数的调用operator(cin,str),它的返回值是istream& ,平时使用while返回值都是整形什么的,这里返回值是一个对象的引用,那对象怎么做判断值的?

其实这里调用了一个函数重载:它会自动的把判断里的值转化为bool值

举例说明:  对象作为判断值

#include
#include
using namespace std;
class B
{

public:
	operator bool()const
	{
		return _a != 0;
	}
public:
	int _a;
};
int main()
{
	B b;
	while (b) //这里进行对象的判断,这里会去调用operator bool() ,b.operator()
	{
		cin >> b._a;
	}
	return 0;
}

输入0就结束了: 

2. fstream (文件流)

 C语言的文件流:文本形式

#include
#include
using namespace std;

struct ServerInfo
{
	char _ip[32]; // ip
	int _port; // 端口
};
void testC_wtext()//写
{
	ServerInfo info = { "192.168.0.1", 2 };
	FILE*fp = fopen("test.txt","w");
	fprintf(fp,"%s %d",info._ip,info._port);//记得要以空格分开,fscanf是以空格或换行为分隔符的
	fclose(fp);
}

void testC_rtext()//读
{
	ServerInfo info = {};
	FILE*fp = fopen("test.txt", "r");
	fscanf(fp,"%s%d",info._ip,&info._port);
	fclose(fp);
	printf("%s:%d", info._ip, info._port);
}
int main()
{
	testC_wtext();
	testC_rtext();
	return 0;
}

C++的文件流 ifstream/ofstream

二进制:

#include
#include
#include
using namespace std;
struct ServerInfo
{
	char _ip[32]; // ip
	int _port; // 端口
};

class  ConfigManager
{
public:
	ConfigManager(const char* s)
		:_filename(s)
	{}
	void WriteBinary(const ServerInfo& info)
	{
		//二进制方式打开写入
		ofstream ofs(_filename.c_str(), ios_base::out|ios_base::binary);
		ofs.write((const char*)&info, sizeof(ServerInfo));
	}
	void ReadBinary(ServerInfo& info)
	{
		//二进制方式打开读取
		ifstream ifs(_filename.c_str(), ios_base::in | ios_base::binary);
		ifs.read((char*)&info, sizeof(ServerInfo));
	}

private:
	string _filename;
};

int main()
{
	ServerInfo info = {"hello world", 2 };
	ServerInfo tmp;
	ConfigManager cm("1.txt");
	cm.WriteBinary(info);
	cm.ReadBinary(tmp);
	cout << tmp._ip << " " << tmp._port << endl;
	return 0;
}

 文本:

#include
#include
#include
using namespace std;

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

class  ConfigManager
{
public:
	ConfigManager(const char* s)
		:_filename(s)
	{}
	//文本形式
	void WriteText(ServerInfo& info)
	{
		ofstream ofs(_filename.c_str());//默认的就是文本
		ofs << info._ip <<" "<< info._port; //写(这里空格不能少,做分割)
		ofs << info.d;
	}
	void ReadText(ServerInfo& info)
	{
		ifstream ifs(_filename.c_str());//默认就是文本
		ifs >> info._ip >>info._port;//读
	}

private:
	string _filename;
};

int main()
{
	ServerInfo info1 = { "1234", 7};
	ServerInfo tmp1;
	ConfigManager cm1("2.txt");
	cm1.WriteText(info1);
	cm1.ReadText(tmp1);
	cout << tmp1._ip << " " << tmp1._port << endl;
	return 0;
}

对于自定义类型:

首先看一下ostream 中重载运算符,对于内置类型使用的是ostream :发生了切片:子类ofstream对象传给了父类的引用。

 再看继承关系:iostream和和ofstream和ostringstream 都继承了ostream

所以在重载自定义类型的数据的时候不能使用ofstream:

ofstream& operator<<(ofstream& ofs ,Date& d)
{
	ofs << d._year << " " << d._month << " " << d._day;
	return ofs;
}

这里会发生报错:内置类型的运算符函数重载返回的ostream,<

 

对于自定义类型的运算符重载:

#include
#include
#include
#include
using namespace std;

class Date
{
	friend ostream& operator<<(ostream& out, Date& d);

public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{

	}
private:
	int _year;
	int _month;
	int _day;
};

//自定义类型运算符重载
ostream& operator<<(ostream& out ,Date& d)
{
	out << d._year <<" "<< d._month <<" "<< d._day << endl;//数据之间用空格分隔
	return out;
}


struct ServerInfo
{
	char _ip[32]; // ip
	int _port; // 端口
	Date d; //自定义类型
};

class  ConfigManager
{
public:
	ConfigManager(const char* s)
		:_filename(s)
	{}
	//文本形式
	void WriteText(ServerInfo& info)
	{
		ofstream ofs(_filename.c_str());//默认的就是文本
		ofs << info._ip << " " << info._port <<" "<< info.d; //写(这里空格不能少,做分割)
	}
	void ReadText(ServerInfo& info)
	{
		ifstream ifs(_filename.c_str());//默认就是文本
		ifs >> info._ip >> info._port;//读
	}

private:
	string _filename;
};

int main()
{
	ServerInfo info1 = { "1234", 7};
	ServerInfo tmp1;
	ConfigManager cm1("2.txt");
	cm1.WriteText(info1);
	cm1.ReadText(tmp1);
	cout << tmp1._ip << " " << tmp1._port <<" "< 

 

 

3.stringstream(字符串流)
#include
#include
#include
using namespace std;

struct PersonInfo
{
	string _name;
	int _age;
};
int main()
{
	//转化成字符串   //也叫序列化
	PersonInfo info = {"张三",18};
	ostringstream oss;
	oss << info._name <<" "<< info._age;//这里也必须使用空格
	string str = oss.str();
	cout << str << endl;


	//将字符串转化成对应的值   //也叫反序列化
	istringstream iss(str);
	string name;
	int age;
	iss >> name >> age;
	cout << name << endl;
	cout << age << endl;

	return 0;
}

这里也不能使用ostingstream:和上面继承是一样的问题。

ostringstream& operator<<(ostringstream& out ,Date& d)
{
	out << d._year <<" "<< d._month <<" "<< d._day << endl;
	return out;
}

对于自定义类型的数据和上面文本写是一样的共用一个:很方便啊。

//自定义类型运算符重载
ostream& operator<<(ostream& out ,Date& d)
{
	out << d._year <<" "<< d._month <<" "<< d._day << endl;
	return out;
}

 对于自定义类型:

#include
#include
#include
using namespace std;
class Date
{
	friend ostream& operator<<(ostream& out, 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)
	{

	}
private:
	int _year;
	int _month;
	int _day;
};

//自定义类型运算符重载
ostream& operator<<(ostream& out ,Date& d)
{
	out << d._year <<" " <>(istream& in, Date d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

struct PersonInfo
{
	string _name;
	int _age;
	Date d;
};
int main()
{
	//转化成字符串
	PersonInfo info = {"张三",18};
	ostringstream oss;
	oss << info._name <<" "<< info._age<<" "<> name >> age>>d;
	cout << name << endl;
	cout << age << endl;
	cout << d << endl;
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010174.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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