Home arrow C++ arrow Page 3 - Using Stringstreams in C++
C++

Using Stringstreams in C++


Variety is the spice of life; if you could see the same things each day, drink the same coffee, meet the same people, eat the same things, life would be absolutely boring. The drawback of this, on the other hand, is that coordinating such variety can be a big challenge. Copied from the multiplicity in our world, we have a significant diversity of types within the C++ programming world. This is not always a good thing. This article will explain how to tackle this challenge with help from streams and the STL class.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
May 12, 2009
TABLE OF CONTENTS:
  1. · Using Stringstreams in C++
  2. · Using It for Input
  3. · For Output
  4. · Free Automatic Conversion

print this article
SEARCH DEVARTICLES

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



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