C++
  Home arrow C++ arrow Page 2 - Iostream Library and Basic I/O in C++
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++

Iostream Library and Basic I/O in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-03-24

    Table of Contents:
  • Iostream Library and Basic I/O in C++
  • The Iostream Library
  • Basic I/O in C
  • 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


    Iostream Library and Basic I/O in C++ - The Iostream Library


    (Page 2 of 4 )

    Briefly sketched, the relations between the classes within the library look like this:

    -> The picture is property of the IBM XL C/C++ V9.0 for AIX help <-


    The upper classes are just type definitions for the char type. There exists also a wchar_t (wide/UNICODE) correspondent for each one, as all of this is written commonly in a basic_ side for each one, as you may see if you open up the iosfwd header:


    // char TYPEDEFS

    typedef basic_ios<char, char_traits<char> > ios;


    // wchar_t TYPEDEFS

    typedef basic_ios<wchar_t, char_traits<wchar_t> > wios;


    This template build assures the stream functionality in a type-independent manner. Each one has two parameters: the type of the stream in the first and the additional character-dependent specifics in the second char_traits parameter.

    To complete the upper picture we must explain that ios itself is derived from ios_base, that, on the other hand, is type-independent and therefore does not has a basic pair.

    The narrow kind of chars are better known, but slowly the world should start moving on the wide char as mentioned in the Introduction to Streams article, for practice- and money-oriented reasons. These are used in the same manner as the narrow ones; it's just that the name/definition is started with the w char.

    To increase portability inside the upstream classes, you will barely find any proper fundamental types in their member prototypes. Instead, they have members like the ones used for the stream position, offset; the defined size depends on the char traits (the second parameter in the template).

    The manipulators are functions that take a parameter that will modify based on  the property of the stream. With these you can use formats similar to the sprint in C++, for instance, or automatic conversion from the stream container upon reading, applied using the >> operator.

    Now we need to look over the upper diagram and see what each one should do. For a start, there are the ios, istream, ostream, streambuf classes that are rarely used directly by most programmers. They represent the base for the rest, or do the work in the background and are automatically included inside the derived classes, so you do not need to be concerned with them. 

    Thefstreampart is responsible for the file input/output using the streams. While the fstream is able to handle both the Input and Output work, there exists theifstream/ofstreamthat allows data flow only in either the inward or outward direction. Using these, you can make sure that you will not mess up and do a task you should not -- like trying to read a value when you should print it.

    If you've heard about STL before, you probably heard about the string type/class. If you did not, no problem; we shall talk about that also. For now it is enough to perceive them as an automatically managed array of chars (wide or narrow). Thesstream is the one responsible, so you can handle this just like a stream.

    Then again, there is theiomanipclass, that contains already-defined manipulators. Though there are quite a few already defined, sometimes you may want to write a new one for yourself. This task will also be discussed in a future article.

    Theiostreamclass finally contains the definition to stream that will enable the communication between the application and the standard input/output using streams. These are all of the standard well-known IOStream objects: cin, cout, cerr, clog, and their wide-character companions, wcin, wcout, wcerr, and wclog.

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek