C++/File/ostrstream
Содержание
construct a table of circular areas
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
using namespace std;
int main()
{
locale usloc("English_US");
ostringstream ostr;
// Give a new, empty string to ostr.
ostr.str(string());
// construct a table of circular areas.
ostr << setprecision(4) << showpoint << fixed << left;
ostr << "Diameter Area\n";
cout << "A table of circular areas.\n";
for(int i=1; i < 10; ++i)
ostr << left << " " << setw(6) << i << setw(8) << right << i*3.1416 << endl;
cout << ostr.str();
return 0;
}
Display the contents of strin via calls to get()
#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";
// use the contents of strout to create strin:
istringstream strin(strout.str());
// Display the contents of strin via calls to get().
cout << "Here are the current contents of strin via get():\n";
do {
ch = strin.get();
if(!strin.eof()) cout << ch;
} while(!strin.eof());
}
Freeze dynamic buffer and return pointer to it
#include <strstream>
#include <iostream>
using namespace std;
main()
{
char *p;
ostrstream outs;
outs << "I like C++ ";
outs.setf(ios::showbase);
outs << 100 << ends;
p = outs.str();
cout << p;
delete p;
return 0;
}
use the contents of strout to create strin
#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";
istringstream strin(strout.str());
do {
ch = strin.get();
if(!strin.eof()) cout << ch;
} while(!strin.eof());
cout << endl;
}