Home arrow C++ arrow Page 2 - Introduction to Streams
C++

Introduction to Streams


C++, unlike C, has the kinds of services and additions that, at the beginning at least, seem like evil dinosaurs meant to make your life harder and to complicate simple problems. However, once you get the hang of them, you’ll realize the opportunities that they give you, and you won’t ever want to use the older C methods for the same purpose again. Streams are one of these wicked dinosaurs.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
March 17, 2009
TABLE OF CONTENTS:
  1. · Introduction to Streams
  2. · Abstract Representation
  3. · How Should They Look?
  4. · Conclusion

print this article
SEARCH DEVARTICLES

Introduction to Streams - Abstract Representation
(Page 2 of 4 )



Think of a stream as a consecutive row of data of n numbers. In every cell you can find the data for a specific type, and get it out from in there, once you take it out from the row in which you put it, if you want. But this can also be true in the case of writing a file.

In the end, a text file can be reduced to a flow of words delimited with white spaces. This is sort of like a container that can assure data flow into and out of it (or in just one direction if that is all we need):


From this point of view, the stream is just a temporary container that holds the data until we “consume it.” A database also offers this option. Likewise, C++ also holds some internal streams for devices we use daily on a computer, like the keyboard and mouse as input streams. But a stream can also be a connection between two computers, a file on the disk, or just a string the memory. C++ tries to offer uniform access to all of these.

Let’s see how can we further expand the use of streams. The best example is to see how the keyboard input stream works, something you've already experienced if you code. Here the stream is actually buffered at two levels. First, the input is buffered by the OS into a file so that you don't need to bother with the char pressing message events, and get the input only when and where you want it.

When you request an input, a buffer will be made with the given size limit; the stream library obtains all data available and stores it in a buffer. By minimizing the input calls, most of the operations can be done on the buffer directly.

In C++, I/O (input/output) occurs in streams of bytes. A stream is simply a sequence of bytes. Input streams take any sequence of bytes from a device, while output streams resolve the same in the opposite direction. Because sometimes both operations may need to be used at the same time, there exists the option that a single stream can handle both (by holding inside both types of streams).

C++ provides both "low-level" and "high-level" ways for manipulating streams. Low level commands simply specify the quantity of the bytes to be received from or sent to the stream. High level I/O, on the other hand (also known as formatted I/O), allows the programmer to consider the bytes in the stream as grouped into something that has its own end-result/type, like Int/for/char, for example.

Files are considered just a sequence of bytes, These may be considered "low-level" when read character-by-character (as in a text file) or "high-level" -- for example, in the case of an int value of a file holding the ‘3’ char. Each file is ended by an end of file (eof) marker to indicate the end of the stream/file.

It's doubtful that anyone still considers this a secret: the father of the C++ language, Bjarne Stroustrup, first realized a version of streams back in 1984. Major contributions were made by Jerry Schwarz and Andrew Koenig (in 1989 – better performance and manipulators) and major revisions were made during the ISO C++ standardization process (like adding the support for the local character set).


blog comments powered by Disqus
C++ ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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