sstream和strstream以及fstream

sstream和strstream以及fstream

在C++有两种字符串流,也称为数组I/O流,一种在sstream中定义,
另一种在strstream中定义。
它们实现的东西基本一样。

strstream里包含
class strstreambuf;
class istrstream;
class ostrstream;
class strstream;
它们是基于C类型字符串char*编写的

sstream中包含
class istringstream;
class ostringstream;
class stringbuf;
class stringstream;
class …….
它们是基于std::string编写的

因此ostrstream::str()返回的是char*类型的字符串
而ostringstream::str()返回的是std::string类型的字符串

在使用的时候要注意到二者的区别,一般情况下推荐使用std::string类型的字符串
当然如果为了保持和C的兼容,使用strstream也是不错的选择。
但要记住一点,strstream虽仍然是C++语言标准的一部分,但已被C++标准宣称为“deprecated”,也就是不再提倡使用了,也说不定以后干粹就没了。 
先介绍一下sstream
//stringstream流以空格为边界符,使用其须包含sstream头文件
//istringstream 用法
istringstream istring;
string ss("ss 8346520");
istring.str(ss);
int i=0;
string s;
istring>>s>>i;
cout<<s<<" "<<i<<endl;
或者
istringstream istring("ss 8346520");
int i=0;
string s;
istring>>s>>i;
cout<<s<<" "<<i<<endl;
都将打印 s内容是ss,i内容是8346520的结果;

//ostringstream 用法
string s="test";
int i=8346520;
int j=0;
string s1;
ostringstream ostring;//不能写成ostringstream ostring<<s<<" "<<i;
ostring<<s<<" "<<i;
cout<<ostring.str()<<endl;//ostring流内保存内容是 test 8346520
istringstream istring(ostring.str());
istring>>s1>>j;//要注意此处的顺序;
cout<<s1<<"――――"<<j<<endl;
简单说说strstream:
基于数组的类有istrstream、ostrstream和strstream。它们分别用来创建输入、输出和输
入/输出流。这些类的基类之一是strstreambuf,它定义了派生类使用的几个底层的具体属
性。
除了strstreambuf以外,istream 也是istrstream的基类。类ostrstream包括了类ostream。
strstream也包括了类iostream。所以,所有基于数组的类和“普通”I/O类一样存取相同的成
员函数。
创建基于数组的输出流
要将一个输出流和一个数组关联起来,可使用下列ostream的构造函数:
ostrstream ostr(char *buf, int size, int mode=ios::out);
其中,buf是指向数组的指针,该数组接收写入流的字符。数组的长度由参数size确定。缺省
时,流以输出方式打开,但也可以将几项或在一起复合为所需的方式(例如,可以包含ios::
app使输出添加在数组中已存在的信息的尾部)。mode的缺省值可以满足大多数的要求。
一旦打开了一个基于数组的输出流,所有对这个流的输出就放在数组中。但是,任何输
出都不能写到数组的限界之外,任何这种企图都会导致错误。
下面是一个介绍基于数组的输出流的简单程序。 
#include <iostream>
#include <strstream>
using namespace std;
int main()
{
int arraysize=50;
char *pbuffer=new char[arraysize];
ostrstream ostr(pbuffer,arraysize,ios::out);
ostr<<"Hello"<<" ";
ostr<<99-14<<hex<<" ";
ostr.setf(ios::showbase);
ostr<<100<<ends; //使用ostrstream输出到流对象的时候,要用ends结束字符串
cout<<pbuffer;
delete[] pbuffer;
return 0;
}
使用数组作输入:
要将输入流和数组关联起来,可使用下列istrstream的构造函数:
istrstream istr(char*buf);
其中,buf是指向数组的指针,该数组作为每次向流输入的字符源。 buf所指的数组必须以空
结束。空结束符从不从数组中读取。
下面是一个用字符串输入的例子:
#include <iostream>
#include <strstream>
using namespace std;
int main()
{

const char s[]="10 Hello 15 12.23 done";
istrstream ins(s);
int i;
char str[80];
float f;
//reading: 10 Hello
ins >>i;ins >>str;
cout<<i<<" "<<str<<endl;
// reading:f 12.23 done.
ins>>i;
ins>>f;
ins>>str;
cout<<hex<<i<<" "<<f<<" "<<str;
return 0;
}
最后是文件i/o流:
//ifstream 用法 须包含fstream头文件
ifstream infile("in.txt");//或者ifstream infile;infile.open("1.txt");
infile.close();//fstream对象只能与1个文件关联,所以打开另外一个,必须关闭上一个;
infile.clear();//如果是循环创建fstream流的话,.close()和clear()函数就应该重视;
infile.open("d:/in1.txt");//文件夹地址应使用反斜杠;
if(infile){//使用前检测状态是个好习惯
string s;
infile>>s;//将读取文件里第1个单词;getline(infile,s);将读取文件里的第1行
cout<<s<<endl;
}

//ofstream 用法
这里涉及很多文件模式,如下:
in――――――――――打开文件做读操作
out――――――――打开文件做写操作
app――――――-添加模式:在每次写之前找到文件尾
ate――――――――打开文件后立即将文件定位在文件尾
trunc――――-打开文件时清空已存在的文件流
binary――――以二进制模式进行IO操作
out,trunc,app只能用于ofstream和fstream对象关联的文件;
in模式只使用于ifstream和fstream对象关联的文件
所有的文件都可以用ate或binary模式打开;
默认时:ifsteam对象关联的文件以in模式打开;ofstream对象关联的文件以out模式打开,以out模式打开的文件会被清空;
string file="d:/out.txt";
ofstream outfile(file.c_str());//默认模式
outfile<<"hello world\n";
outfile.close();//注意close;
outfile.clear();
outfile.open(file.c_str(),ofstream::app);//显式更改文件模式,添加模式
outfile<<"\nhello world again!";
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值