Home arrow C++ arrow 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++
(Page 1 of 4 )

The key area in the streams is the buffer area that is included in the streambuff class. Most of the time this function works in the background, accomplishing  tasks that are a little more complex. Perceive this as an internal class that does the heavy work, while the rest of the classes that you declare when working with streams are just specializations of this one.

For a start, let's see how the buffer area works and how it is constructed to assure an efficient build up of the streams. Before we move on, let me specify that although this article is part of my series about streams, it will be a little more advanced. At least a good knowledge of C and a strong OOP background are required if you aim to completely understand and grasp my words. 

This content of this article may not even be useful to you unless you care about a specific mode for performance. The polymorphic power of C++ gives you the possibility to use it in a multitude of ways, and you may find a different approach paired with a good solution using the already-defined streams. Nevertheless, this article will give you a greater insight into how it works behind the scene. If you are interested, just read on and be amazed.

Read my introduction to streams; once you're done we can move on. As mentioned during the previous article of this series (Basic I/O), the ofstream, ifstream are just typedefs of the basic_isftream/basic_ofstream with the template parameter "char." Those were made to attract newcomers to the library and so that you will write less.

So in fact, this brings us to the point that we can declare the basic_ifstream to any type: for example, on a Car Type. The compiler might not even complain about it (this is subject to how that specific class is constructed). However, once you start calling functions around, serious errors will pop up.

The problem is simply that there is no trait_type structure declared for a Car Type. In the basic package of the library, the C++ programming guide defined only the wide and narrow characters. This you will find in the iosfwd file input. Writing a specialization for the Car Type can also be troublesome; it will prove to be simpler to go even further in the derivation list of the basic_ifstream and find that the road leads to the filebuffer.

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