首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Symbian >> 语言基础 >> 正文
C++的iostream标准库介绍(5)
作者:管宁    文章来源:PConline    更新时间:2006-5-30 15:12:47
类istrstream的构造函数原形如下:
  istrstream::istrstream(const char *str,int size);
  参数1表示字符串数组,而参数2表示数组大小,当size为0时,表示istrstream类对象直接连接到由str所指向的内存空间并以\0结尾的字符串。

  下面的示例代码就是利用istrstream类创建类对象,制定流输入设备为字符串数组,通过它向一个字符型对象输入数据。

  代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
#include <iostream
#include <strstream
using namespace std; 
int main()  

    char *name = "www.cndev-lab.com"; 
    int arraysize = strlen(name)+1; 
    istrstream is(name,arraysize); 
    char temp; 
    is>>temp; 
    cout<<temp; 
    system("pause"); 
}

  类ostrstream用于执行串流的输出,它的构造函数如下所示:

  ostrstream::ostrstream(char *_Ptr,int streamsize,int Mode = ios::out);

  第一个参数是字符数组,第二个是说明数组的大小,第三个参数是指打开方式。

  我们来一个示例代码:

#include <iostream
#include <strstream
using namespace std; 
int main()  

    int arraysize=1; 
    char *pbuffer=new char[arraysize]; 
    ostrstream ostr(pbuffer,arraysize,ios::out); 
    ostr<<arraysize<<ends;//使用ostrstream输出到流对象的时候,要用ends结束字符串 
    cout<<pbuffer; 
    delete[] pbuffer; 
    system("pause"); 
}

  上面的代码中,我们创建一个c风格的串流输出对象ostr,我们将arraysize内的数据成功的以字符串的形式输出到了ostr对象所指向的pbuffer指针的堆空间中,pbuffer也正是我们要输出的字符串数组,在结尾要使用ends结束字符串,如果不这么做就有溢出的危险。

相关文章:
C++的iostream标准库介绍(7)
C++的iostream标准库介绍(6)
在S60中的图象处理需要注意的几点问题
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号