C++
  Home arrow C++ arrow Page 2 - Extending the Basic Streams 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++

Extending the Basic Streams in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-05-19

    Table of Contents:
  • Extending the Basic Streams in C++
  • The Source of it All
  • The streambuff Class
  • Epilogue

  • 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


    Extending the Basic Streams in C++ - The Source of it All


    (Page 2 of 4 )

    First, let me tell you that there are three types of streams in C++, namely the fstream (for file I/O), the sstream (for C type character sequences - char*- however this is a leftover from the days of C++ standardization for compatibility reasons and should not be used, as support for this in the future may be removed; this isn't a template class) and strstream (for the string type of the STL library). Now all of these will have in common the fact that the text stored eventually is a text. That is why the following code snippet succeeds in execution:



    #include <iostream>

    #include <sstream>

    #include <strstream>


    using namespace std;


    char alfa = 'A';


    template < typename _S>

    std::basic_ostream<_S>& fillWithText (std::basic_ostream<_S>& stream)

    {

    stream << "Filled with text the " << alfa++ << "Stream"<< std::endl;

    return stream;

    }


    int main()

    {

    wostringstream A;

    wostream& B = std::wcout;

     

    fillWithText<wchar_t>(A);

    std::wcout << A.str();

     

    fillWithText<wchar_t>(B);


    }


    Moreover, the result is:


    Filled with text the A Stream

    Filled with text the B Stream

    Press any key to continue . . .


    First, let's point out that you can assign a string to the standard input/output, and in the future thread it just as the cout stream class. Second, look at the way we printed the A stream. The str() function returns a pointer to the character sequence inside the stream itself, though it will print all that we put inside it in the meantime.

    The existence of this pointer can be reduced to the inside representation of a stream. The streambuff class handles this. A schematic representation looks like the figure below.



    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