C++
  Home arrow C++ arrow Page 3 - Using Stringstreams 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++

Using Stringstreams in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-05-12

    Table of Contents:
  • Using Stringstreams in C++
  • Using It for Input
  • For Output
  • Free Automatic Conversion

  • 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


    Using Stringstreams in C++ - For Output


    (Page 3 of 4 )

    We saw in the previous example how we could use a stringstream for input, so what is the point of using one for output? "Where can we use it?" You may ask. Before we venture into this, let me tell you that when you insert new members into a stream, if you want to extract them from there later or just make it more readable, you must add white spaces inside the stream.

    These white spaces delimit the members inside a stream by default. In addition, everything we learned about streams applies here, such as the manipulators and the insertion/extraction process for your own custom-made class. If you are not familiar with either one of these concepts, go to my profile (click on my name at the top of this page), where you can easily find the list of my already-published articles. You should have no problem locating the ones where I deal with these concepts.

    You can request an old style char* pointer to the strings buffer with the member function str(). Dumping all of your data inside a stringstream and calling a single print at the end is perfectly maintainable:


    ostringstream memoryStream;

     

    int number = 10;

    double dnubmer = 10.122;

    string text = "nalfa";


    memoryStream << number << ' ' << dnubmer << text;


    cout << memoryStream.str() << endl;


    Remember that a white space separates each item, and that print is an effect of this. No additional white space is added automatically during or after the insertion process. All this is left for you to do, if need be. The str() member function returns, in fact, a character sequence of exactly what you inserted in the string, nothing more and nothing less.


    10 10.122

    alfa

    Press any key to continue . . .


    Be aware that this stream is not error safe. Just as a stream will put its fail flag on once you try to read a text inside an integer, this will do the same. So watch out and whenever an error can occur, check it with the flag() member functions, and if it is small, reset it with the clear().

    To prove this, look over the code below and the output. No change was made during the extraction once we managed to call an invalid operation.


    stringstream memoryStream;

     

    int number = 10;

    double dnubmer = 10.122;

    string text = "alfa ";


    memoryStream << text << number << ' ' << dnubmer ;

     

     

    cout << memoryStream.str() << endl << "nExtraction:n";

     

    //reset the values

    number = 0;

    dnubmer = 0;

    text.clear();


    //reassign

    memoryStream >> number;

    memoryStream >> text;

    memoryStream >> dnubmer;

     

    cout << number << endl << text << endl << dnubmer << endl;



    alfa 10 10.122


    Extraction:

    0


    0

    Press any key to continue . . .


    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