C++
  Home arrow C++ arrow Page 2 - Introduction to Streams
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Introduction to Streams
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2009-03-17

    Table of Contents:
  • Introduction to Streams
  • Abstract Representation
  • How Should They Look?
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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).

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek