Write to strstream - C++ File Stream

C++ examples for File Stream:stream

Description

Write to strstream

Demo Code

#include <iostream>
#include <strstream>
#include <iomanip>
using namespace std;
int main()//from w w  w  .j a  v  a2 s  .c om
{
   const int MAXCHARS = 81;  // one more than max characters in a line
   int units = 10;
   double price = 36.85;
   char buf[MAXCHARS];
   strstream inmem(buf, MAXCHARS, ios::out);  // open in-memory stream
   // Write to the buffer through the stream
   inmem << "No. of units = " << setw(3) << units << "  Price per unit = $" << setw(6) << setprecision(2) << fixed << price << '\0';
   cout << '|' << buf << '|';
   cout << endl;
   return 0;
}

Result


Related Tutorials