C++ examples for File Stream:stream
Use a string stream to create formatted string
#include <iostream> #include <sstream> using namespace std; int main()//from w w w. j a v a2s . co m { char ch; ostringstream strout; cout << "Use an output string stream called strout.\n"; // Write output to the string stream.. strout << 10 << " " << -20 << " " << 30.2 << "\n"; strout << "This is a test."; cout << "The current contents of strout as obtained from str():\n" << strout.str() << endl; strout << "\nThis is some more output.\n"; cout << endl; cout << "Use an input string stream called strin.\n"; // Now, 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()); cout << endl; cout << "Now, use an input/output string stream called strinout.\n"; stringstream strinout; strinout << 10 << " + " << 12 << " is " << 10+12 << endl; cout << "Here are the current contents of strinout via get():\n"; do { ch = strinout.get(); if(!strinout.eof()) cout << ch; } while(!strinout.eof()); cout << endl; // Clear eofbit on strinout. strinout.clear(); strinout << "More output to strinout.\n"; // The following will continue reading from the point // at which the previous reads stopped. cout << "Here are the characters just added to strinout:\n"; do { ch = strinout.get(); if(!strinout.eof()) cout << ch; } while(!strinout.eof()); }