輸入輸出的標頭檔 <iostream>
string流的標頭檔 <sstream>
檔案流的標頭檔 <fstream>
stringstream的用法
1.利用輸入輸出做資料轉換
stringstream ss_stream;
ss_stream << i; // 將int輸入資料流中
ss_stream >> str; // 將ss_stream中的數值輸出到str中
//注意:如果做多次資料轉換;必須調用clear()來設定轉換模式
ss_stream << "456";
ss_stream >> i; // 首先將字串轉換為int
ss_stream.clear();
ss_stream << true;
ss_stream >> i; // 然後將bool型轉換為int;假如之前沒有做clear,那麼i會出錯
//運行clear的結果
i = 456
i = 1
//沒有運行clear的結果
i = 456
i = 8800090900
2.支援char*的輸入和輸出
char sz_buf[20];
ss_stream << 8888;
ss_stream >> sz_buf; // 直接將數輸出到sz_buf字元數組中
3.來儲存可變資料的列表
stringstream ss_stream;
ss_stream << "字串一" << endl;
ss_stream << "字串二" << endl;
ss_stream << "字串三" << endl;
ss_stream << "字串四" << endl;
ss_stream << "字串五" << endl;
char buffer[100];
while ( ss_stream.getline(buffer, sizeof(buffer))
{
printf("msg=%s\n", buffer);
}
ss_stream("");// 釋放字串流中的資源
// 或者用string來接收
stringstream ss_stream;
string stemp;
while ( getline(ss_stream, stemp) )
{
task_download(stemp.c_str(), relate.c_str());
}