C++文件流、字符串流

字符串流stringstream

#include <iostream>
#include <stdexcept>
#include <exception>
#include <vector>
#include <fstream>
#include <sstream>


using namespace std;

ifstream in;


void except(){
	try{
		in.open("C:\\Users\\done1\\Desktop\\gcc.txt", ios::in);
	}catch(...){
		throw "NOT Found File";		
		cout << "Not Found File" <<endl;
	}


}
struct Person{
	string name;
	vector<string> phones;
	
};
int main(){
	char a,b;

	string str,word,str1,str2 = "sssssss";
	vector<Person> people;
	Person pl;
	
	
//	cin >> a;
//	cout << a << "\n";
//	cin >> b;
//	cout << b << endl;
	
	in.open("C:\\Users\\done1\\Desktop\\gcc.txt", ios::in);
	if(!in){
		cout << "NOT FOUND FILE"<<endl;
	}
	cout << "cin str Origin data:";
	cin >> str;
	cout << "str Origin data:"<< str << endl;
	
	cout << "----------------------------------------"<< endl;
	
	stringstream strm(str);  //定义字符串流,并绑定字符串str,字符串str的内容被复制到strm的字符串流中
	stringstream strm1;
	//将字符串str拷贝到字符串流中,当定义字符串流位绑定字符串时,使用str()方法来绑定,str()其实被重载了
	
	str = strm.str();  //返回字符串流strm中存储字符串的拷贝,
	cout << "get str from strm,Origin data:"<< str <<endl;
	
	stringstream strNo;  //定义字符串流,未绑定字符串,使用str()方法初始化
	strNo.str(str2);  //等同于stringstream strNo(str2)
	//修改str的内容,将strNo中字符串,赋给str
	strNo >> str; //将字符串流中的内容,写入到str字符串中,会修改原来的字符串内容
	cout << "update str data by strNo:"<< str <<endl;
	cout << "get str from strNo:" << strNo.str() <<endl;
	
	cout << "----------------------------------------"<< endl;
	
	strNo.str(""); //字符串流清除
	strNo.clear();  //单独使用clear不会清除字符串流,如果想清空 stringstream,必须使用 sstream.str(""); clear() 方法适用于进行多次数据类型转换的场景
	
	string strtemp;
	strNo << "11111";  //将 << 后的字符串写入到字符串流中
	strNo >> strtemp;  //将字符串流中内容赋给 << 后的内容
	cout << "clear stream and revalue by << " << strNo.str() <<endl;
	cout << "modify string by stream >> " << strtemp << endl;
	
	cout << "----------------------------------------"<< endl;
	
	cout << "get pname and phone from strm :";
	str = strm.str();
	cout << str << endl;
	strm >> pl.name;  //将字符串流中复制字符串 赋给 >> 后的值

	strm1 << "111";
	strm1 >> word;
	
	cout << "name " << pl.name <<endl;
	cout << "phone " << word <<endl;
	
	pl.phones.push_back(word);
	people.push_back(pl);
	
	str = people[0].name;
	cout << "str name " << str <<endl;
	str = people[0].phones[0];
	cout << "phone " << str <<endl;
	
	cout << "----------------------------------------"<< endl;
			
	except();
	in.close();

	return 0;
}

str()  VS clear()

#include <iostream>
#include <sstream>
using namespace std;

int main(){
	stringstream strm;
	int i = 0;
	string str="str NOT NULL",str1,str2,str3="str3 NOT NULL";
	strm.str("1111"); //与 strm << "111111"; 结果相同
	//	strm.str(1111); //错误,使用str只能拷贝字符串到字符串流中,不能使用整形数据
	//字符串流程序运行时只能使用一次,执行此语句后str1和strm建立连接,所以strm无法再和其他字符串建立连接
	strm >> str1;
	cout << "str1 = "<< str1 << endl;
	
	strm >> str3; //此处str3无法和strm建立连接,就无法将strm中到内容拷贝到str3中
	cout << "str3 NOT modify,keep origin data: " << str3 << endl;

	strm.str(""); //清空strm字符串流,直接将字符串流内容清空,无法通过<<向流中写入内容,只能使用str()初始化	
	str = strm.str(); //strm中的内容被清除
	if(str.length() == 0)cout << "str become NULL" <<endl;
//	重新初始化strm
	strm.str("sssss"); //strm.str(""); 
	
	strm.clear();//若要修改字符串流中的内容必须使用clear,清空字符串流中错误标记,原内容不清空,后续可以使用<<持续输入到流中	
	strm >> str2;  //使用clear后可以重新将字符串流内容写入字符串中
	i = str2.length();
	if(i)cout << "str2 NOT NULL:" << str2 <<endl;
	
	//使用clear()后,strm中的内容仍保持原来的值
	str = strm.str();
	cout << str <<endl;
	
	strm.clear();
	strm << 111; //若要使用 << 给字符串流中重新赋值,就必须执行上面clear语句
	strm << " 2222 " << i;//使用<<可将任意类型数据一直输出到字符串流中
	cout << strm.str()<<endl;

	cout << str2 <<endl;
	
	return 0;
}

参考:https://blog.csdn.net/lanchunhui/article/details/50731706?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值