C++/File/String stream
Версия от 14:21, 25 мая 2010; (обсуждение)
create string stream for input/output
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
char ch;
stringstream strinout;
strinout << 10 << " + " << 12 << " is " << 10+12 << endl;
do {
ch = strinout.get();
if(!strinout.eof()) cout << ch;
} while(!strinout.eof());
cout << endl;
strinout.clear();
strinout << "More output to strinout.\n";
cout << "the characters just added to strinout:\n";
do {
ch = strinout.get();
if(!strinout.eof()) cout << ch;
} while(!strinout.eof());
}
Demonstrate string streams.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream s("This is initial string.");
// get string
string str = s.str();
cout << str << endl;
// output to string stream
s << "Numbers: " << 10 << " " << 123.2;
int i;
double d;
s >> str >> i >> d;
cout << str << " " << i << " " << d;
return 0;
}
string stream classes in action.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
char ch;
ostringstream strout;
strout << 10 << " " << -20 << " " << 30.2 << "\n";
strout << "This is a test.";
cout << strout.str() << endl;
strout << "\nThis is some more output.\n";
cout << "Use an input string stream called strin.\n";
istringstream strin(strout.str());
cout << "Here are the current contents of strin via get():\n";
do {
ch = strin.get();
if(!strin.eof()) cout << ch;
} while(!strin.eof());
stringstream strinout;
strinout << 10 << " + " << 12 << " is " << 10+12 << endl;
do {
ch = strinout.get();
if(!strinout.eof()) cout << ch;
} while(!strinout.eof());
cout << endl;
strinout.clear();
strinout << "More output to strinout.\n";
do {
ch = strinout.get();
if(!strinout.eof()) cout << ch;
} while(!strinout.eof());
}