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!";