/*string 流演示*/
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
#include <cfloat>
#include <iomanip> //包含控制符
#include <sstream>
using namespace std;
int main()
{
string date="U.S. independence: July 4, 1776";
//用string date 创建一个istringstream
istringstream istr(date);
string word1,word2,month;
int day,year;
char comma;
//用输入运算符>>将它分解为单独的符号,其中一些为单词,而其他的为整数
istr>>word1>>word2>>month>>day>>comma>>year;
cout<<"Contents of string stream istr, one word per line:\n"
<<word1<<'\n'<<word2<<'\n'<<month<<'\n'<<'\n'
<<day<<comma<<'\n'<<year<<'\n'<<endl;
ofstream outfile("data3.txt");
assert(outfile.is_open());
ostringstream ostr;
//产生一个格式化的输出到 ostr
ostr<<word1<<"bicentennial: "<<month
<<setw(2)<<day<<", "<<year+200<<endl;
//str()创建一个ostream中字符串的拷贝以及一个ofstream中字符串的拷贝
cout<<"Contents of string steam ostr:\n"<<ostr.str();
outfile<<ostr.str();
outfile.close();
return 0;
}
输出文件:data3.txt
U.S.bicentennial: July 4, 1976