Home arrow C++ arrow Page 3 - Extending the Basic Streams in C++

Extending the Basic Streams in C++

The basic stream library (iostream) is designed to resolve most of the general problems you may run into during your programming endeavors. However, sometimes it simply isn't enough, and you need to extend it to suit your application—your special situation. This article will show you how to do that.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
May 19, 2009
  1. · Extending the Basic Streams in C++
  2. · The Source of it All
  3. · The streambuff Class
  4. · Epilogue

print this article

Extending the Basic Streams in C++ - The streambuff Class
(Page 3 of 4 )

The streambuffer has the following definition once you open up the streambuf header:

class basic_streambuf

{ // control read/write buffers




: _Plocale(_NEW_CRT(locale))

{ // construct with no buffers





_Elem *eback() const;

_Elem *gptr() const;

_Elem *pbase() const;

_Elem *pptr() const;

_Elem *egptr() const;

void gbump(int _Off);

void setg(_Elem *_First, _Elem *_Next, _Elem *_Last)


_Elem *epptr() const;

_Elem *_Gndec();

_Elem *_Gninc();

_Elem *_Gnpreinc();

void pbump(int _Off);

void setp(_Elem *_First, _Elem *_Last);

void setp(_Elem *_First, _Elem *_Next, _Elem *_Last);

_Elem *_Pninc();


void _Init()

{ // initialize buffer parameters for no buffers

_IGfirst = &_Gfirst, _IPfirst = &_Pfirst;

_IGnext = &_Gnext, _IPnext = &_Pnext;

_IGcount = &_Gcount, _IPcount = &_Pcount;

setp(0, 0), setg(0, 0, 0);


void _Init(_Elem **_Gf, _Elem **_Gn, int *_Gc,

_Elem **_Pf, _Elem **_Pn, int *_Pc)

{ // initialize buffer parameters as specified

_IGfirst = _Gf, _IPfirst = _Pf;

_IGnext = _Gn, _IPnext = _Pn;

_IGcount = _Gc, _IPcount = _Pc;



_Elem *_Gfirst; // beginning of read buffer

_Elem *_Pfirst; // beginning of write buffer

_Elem **_IGfirst; // pointer to beginning of read buffer

_Elem **_IPfirst; // pointer to beginning of write buffer

_Elem *_Gnext; // current position in read buffer

_Elem *_Pnext; // current position in write buffer

_Elem **_IGnext; // pointer to current position in read

_Elem **_IPnext; // pointer to current position in write

int _Gcount; // length of read buffer

int _Pcount; // length of write buffer

int *_IGcount; // pointer to length of read buffer

int *_IPcount; // pointer to length of write buffer


This is, of course, a simpler version; I have omitted a lot of functions as these alone are the ones on which we want to focus. The figure at the end of the previous page already gave a hint as to how this is built up, and the code should make that crystal clear.

Any I/O stream will have two buffers: a get area and a put area. An ifstream will have a get, an ofstream will have a put and of course, the iostream will have both of them. The buffers may be overlapped also, as is the case with files(filebuffer).

The base() and ebuf() functions will return the pointers within which the stream buffer holds its data. Inside these, you will find two areas: one for the input data and one for the output streams (get/put).

These on their own are delimited into an area and have a pointer for the current position inside them. On the get area the eback () and egptr () functions delimit the boarders, while the gptr () handles the current positions pointer.

The same rules apply for the put area, delimited by the pbase () and epptr () functions. Returned pointers are separate; pointers returned by the function pptr () will show the current position.

There exist two _Init() functions to handle the initialization of this pointer. The default will just assign each pointer to NULL and will make the stream unbuffered.

Unbuffered streams will not save any of the input directly; rather, they will just push on further for each character. The second function has four pointers to delimit the borders for the put and get area, and in addition, two int* pointers that correspond to the size of the areas.

The two areas are different from each other, and while the one of them is buffered, the other can be unbuffered -- or any other combination you can imagine. When choosing between buffered and unbuffered versions, keep in mind that the buffered version can have many advantages of performance improvement, as it will minimize the number of system calls, as I explained in the Introduction to Streams article.

The determination of the stream you use, whether they are buffered or not, is reduced to checking if the corresponding pointers are or are not null. Of course, putting/reading data from either of the areas is allowed only if the pointers are valid, and they are in the corresponding sequence. For instance, if gptr() < egptr() you are free to read the data, and with it, move after each char and read the current position pointer.

The gbump(), _Gndec(), _Gninc(), _Gnpreinc(), _Pninc(), pbump() functions are for handling these pointers by increasing/decreasing them or more exactly moving the current position pointer in the areas. Here you should notice the lack of the decrease function for the put area.

This is because the developer team thought that this can lead to unexpected errors and did not want to encourage this. You see, this can lead to data being overwritten, and with it, corrupted data for a stream introduced earlier. Reading old data will cause no harm to anyone, but producing data corruption is certainly going to do so.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials