IO流C++

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Enterprise_/article/details/86644155
  1. iostream处理控制台IO
#include<iostream>
#include<string>
using namespace std;
istream& Test(istream &in) {  //IO对象没有拷贝或者赋值,所以形参和返回值都不能设置为流类型,通常用引用来传递流对象。
	string word;
	while (in >> word && !in.eof()) {
		cout << word << endl;
	}
	in.clear();
	return in;
}

void TestCin(istream &in) {
	int a = 0;
	auto old_state = cin.rdstate(); //记住cin的当前状态
	cin.clear(); //无参数clear,将所有条件状态复位
	while (cin >> a) {}
	cin.setstate(old_state);		//将cin设置为原有状态


	//只复位failbit和badbit位,其他不变
	cin.clear(cin.rdstate()  & ~cin.failbit  & ~cin.badbit);

}
int main(void) {
	cout << "a" << endl;//加入回车后立刻刷新
	cout << "a" << ends;//加入空格后立刻刷新
	cout << "a" << flush;//什么也不做立刻刷新

	cout << unitbuf;//所以输出都立刻刷新,无缓冲
	cout << "a";
	cout << nounitbuf;//回到正常的刷新方式
	cout << "a";
	return 0;
}
  1. fstream处理命名文件IO
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
vector<string> str;
vector<string>::iterator it;
string tmp;
void printline(string ifile) {
	ofstream  out;								//未指定文件打开模式
	string    word;
	out.open(ifile + ".txt", ofstream::app);	//设置文件打开模式为app追加模式
	while (getline(cin,word)  && word != "###")	//读入一行
		out << word<<endl;					//写入文件
	out.close();//关闭文件流
}
void printone(string ifile) {
	ofstream  out;								//未指定文件打开模式
	string    word;
	out.open(ifile + ".txt", ofstream::app);	//设置文件打开模式为app追加模式,实际上隐含了out模式,仅以out模式打开文件会丢弃原有数据
	//上述效果等价于out.open(ifile+".txt),ofstream::out | ofstream::app);
	while (cin >> word  && word != "###")		//读入一个
		out << word << endl;					//写入文件
	out.close();								//关闭文件流
}
void readone(string ifile) {//将文件内容读入到一个string的vector容器中
	str.clear();
	ifstream openfile(ifile+".txt",ifstream::in);  //以读模式打开一个文件							
	while(openfile >> tmp) {	//没有到达文件的尾部
			//读入一个
		str.push_back(tmp);		//每个元素单独存入vector中
	}
	if (str.empty()) {			//没有数据,直接返回
		cout << "No data?!" << endl;
		return ;
	}
	it = str.begin();
	for (; it != str.end(); it++)	//输出文件内容(存入vector中)
		cout << (*it) << endl;
	openfile.close();				//关闭文件流
	
}
void readline(string ifile) {//将文件内容读入到一个string的vector容器中去
	str.clear();
	ifstream openfile(ifile + ".txt", ifstream::in);  //以读模式打开一个文件									  
	while (getline(openfile, tmp)) {		//没有到达文件的尾部
				//读入一行
		str.push_back(tmp);			//每一行作为独立元素存入vector中
	}
	if (str.empty()) {				//没有数据,直接返回
		cout << "No data?!" << endl;
		return;
	}
	it = str.begin();	
	for (; it != str.end(); it++)	//输出文件内容(存入vector中)
		cout << (*it) << endl;
	openfile.close();				//关闭文件流
}
int main(void) {
	printone("1");
	readone("1");
	printline("1");
	readline("1");
	return 0;
}
  • 读入过程
    在这里插入图片描述
  • 文件写入后的txt
    在这里插入图片描述
  1. stringstream完成内存string的IO
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;

int main(void) {
	ostringstream os;
	os << "dad";
	string as = os.str();
	string bs = "1213213";
	os << bs;
	cout << as << endl;
	as= os.str();
	cout << as << endl;

	//如果构造的时候设置了字符串参数,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长
	ostringstream temp("1");
	cout << temp.str() << endl;
	temp << "dsadas";		//修改原有数据后追加字符串
	string t1 = temp.str();
	cout << t1 << endl;


	os.clear();//如果需要使用同一个流,每次使用之前都需要clear一下
	int a = 15216;
	stringstream ans;//int转字符串
	string b;
	ans << a;
	ans >> b;
	cout << b << endl;
	return 0;
}

在这里插入图片描述
4. 总结
类fstream和stringstream都是继承类iostream的,输入继承istream,输出继承ostream,所以能都使用istream的地方都可以使用ifstream和istringstream,对ostream同理。

猜你喜欢

转载自blog.csdn.net/Enterprise_/article/details/86644155