Click on the banner to return to the class reference home page.

basic_stringbuf


basic_stringbufbasic_streambuf

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <sstream> 
template<class charT, class traits = char_traits<charT>,
         class Allocator = allocator<void> >
class basic_stringbuf
: public basic_streambuf<charT, traits>

Description

The class basic_stringbuf is derived from basic_streambuf. Its purpose is to associate the input or output sequence with a sequence of arbitrary characters. The sequence can be initialized from, or made available as, an object of class basic_string. Each object of type basic_stringbuf<charT, traits,Allocator> controls two character sequences:

Note: see basic_streambuf.

The two sequences are related to each other, but are manipulated separately. This allows you to read and write characters at different positions in objects of type basic_stringbuf without any conflict (as opposed to the basic_filebuf objects, in which a joint file position is maintained).

Interface

template<class charT, class traits = char_traits<charT>,
         class allocator<void> >
class basic_stringbuf 
: public basic_streambuf<charT, traits> {

  public:

   typedef basic_ios<charT, traits>     ios_type;

   typedef basic_string<charT, traits,
                        Allocator>      string_type;

   typedef charT                        char_type;
   typedef typename traits::int_type    int_type;
   typedef typename traits::pos_type    pos_type;
   typedef typename traits::off_type    off_type;


   explicit basic_stringbuf(ios_base::openmode which =
                            (ios_base::in | ios_base::out));
     
   explicit basic_stringbuf(const string_type& str,
                            ios_base::openmode which =
                            (ios_base::in | ios_base::out));
    
   virtual ~basic_stringbuf();
     
   string_type str() const;     
   void str(const string_type& str_arg);

 protected:

   virtual int_type overflow(int_type c = traits::eof());

   virtual int_type pbackfail(int_type c = traits::eof());

   virtual int_type underflow();

   virtual basic_streambuf<charT,traits>* 
    setbuf(char_type *s,streamsize n);

   virtual pos_type seekoff(off_type off, ios_base::seekdir way,
                            ios_base::openmode which =
                            ios_base::in | ios_base::out);

   virtual pos_type seekpos(pos_type sp,
                            ios_base::openmode which =
                            ios_base::in | ios_base::out);

   virtual streamsize xsputn(const char_type* s, streamsize n);

};

Types

char_type
ios_type
off_type
pos_type
string_type
stringbuf 
traits_type
wstringbuf

Constructors

explicit basic_stringbuf(ios_base::openmode which =
                ios_base::in | ios_base::out);
explicit basic_stringbuf(const string_type& str,
                ios_base::openmode which =
                ios_base::in | ios_base::out);

Destructor

virtual ~basic_stringbuf();

Member Functions

int_type 
overflow(int_type c = traits::eof() );
int_type 
pbackfail( int_type c = traits::eof() );
pos_type 
seekoff(off_type off, ios_base::seekdir way,
        ios_base::openmode which = 
        ios_base::in | ios_base::out);
pos_type 
seekpos(pos_type sp,ios_base::openmode which = 
        ios_base::in | ios_base::out);
basic_streambuf<charT,traits>* 
setbuf(char_type*s, streamsize n);
string_type 
str() const;
void 
str(const string_type& str_arg);
int_type 
underflow();
streamsize 
xsputn(const char_type* s, streamsize n);

Examples

// stdlib/examples/manual/stringbuf.cpp
//
#include<iostream>
#include<sstream>
#include<string>

void main ( )
{
  using namespace std;

  // create a read/write string-stream object on tiny char
  // and attach it to an ostringstream object
  ostringstream out_1(ios_base::in | ios_base::out);

  // tie the istream object to the ostringstream object
  istream in_1(out_1.rdbuf());   

  // output to out_1
  out_1 << "Here is the first output";

  // create a string object on tiny char 
  string  string_ex("l'heure est grave !");

  // open a read only string-stream object on tiny char
  // and initialize it
  istringstream in_2(string_ex);

  // output in_1 to the standard output
  cout << in_1.str() << endl;

  // output in_2 to the standard output
  cout << in_2.rdbuf() << endl;

  // reposition in_2 at the beginning
  in_2.seekg(0);

  stringbuf::pos_type pos;

  // get the current put position
  // equivalent to
  // out_1.tellp(); 
  pos = out_1.rdbuf()->pubseekoff(0,ios_base::cur,
                                  ios_base::out);

  // append the content of stringbuffer
  // pointed at by in_2 to the one 
  // pointed at by out_1
  out_1 << ' ' << in_2.rdbuf();

  // output in_1 to the standard output
  cout << in_1.str() << endl;

  // position the get sequence
  // equivalent to
  // in_1.seekg(pos);
  in_1.rdbuf()->pubseekpos(pos, ios_base::in);

  // output "l'heure est grave !"
  cout << in_1.rdbuf() << endl << endl;  
}

See Also

char_traits(3C++), ios_base(3C++), basic_ios(3C++), basic_streambuf(3C++), basic_string(3C++), basic_istringstream(3C++), basic_ostringstream(3C++), basic_stringstream(3C++)

Working Paper for Draft Proposed International Standard for Information Systems--Programming Language C++, Section 27.7.1

Standards Conformance

ANSI X3J16/ISO WG21 Joint C++ Committee


©Copyright 1996, Rogue Wave Software, Inc.