basic_stringbuf::str

To return or clear the basic_string object stored in the buffer.

  basic_string<char_type> str() const;
  void str(const basic_string<char_type>&s);  
Remarks

The function str() freezes the buffer then returns a basic_string object.

The function str(const string s) assigns the value of the string `s' to the stringbuf object.

The no argument version returns a basic_string if successful. The function with an argument has no return.

Listing: Example of basic_stringbuf::str() usage:
#include <iostream>
#include <sstream>
#include <cstring>

char CW[] = "CodeWarrior";
char AW[] = " - \"Software at Work\""

int main()
{
using namespace std;
  string buf;
   stringbuf strbuf(buf, ios::in | ios::out); 
   int size;
   size = strlen(CW);
   strbuf.sputn(CW, size);
   size = strlen(AW);
   strbuf.sputn(AW, size);   

   cout << strbuf.str() << endl;

   // Clear the buffer then fill it with
   // new information and then display it   
   string clrBuf = "";   
   string ANewLine = "We Listen we Act";
   strbuf.str(clrBuf);
   strbuf.sputn( ANewLine.c_str(), ANewLine.size());
   cout << strbuf.str() << endl;

   return 0;
}

Results

  CodeWarrior - "Software at Work"

  We Listen we Act