Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

Using Stringstreams in C++ | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » Using Stringstreams in C++

Using Stringstreams in C++








Article Posted On Date : Wednesday, May 20, 2009


Using Stringstreams in C++
Advertisements

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

Many beginner coders, in fact, struggle with the conversions from one type to another. Probably the most troublesome problem turns out to be adding numbers of type integer and float to the std class string.

If you've always cursed this process, I invite you to continue reading this article. Even if you haven't, sit back, relax, and observe a solution that is simpler to write and maintain than the ones you may have used inside C (itoa, atoi, and others) until now.

This article is the ninth part of a series about streams in C++. Therefore, if you lack some general knowledge about this or the STL class string, I strongly encourage you to search for and read one or more of my articles about this published here on the Developer Shed Network under the names: Introduction to Streams, Basic IO in C++ or The String STL Class.

Today we will see how we can combine the power of the stream and the advantages of the STL class string together to make conversion of objects inside the C++ a seamless and trivial task.

As you may already recognize the benefits of streams as an easy way to read and write data with just the use of the insertion and extraction operator (<< and >>), the profit from strings is obvious: they offer an array of chars that does not need buffer management.

Working with streams to handle files has proven to be an efficient and comprehensible approach for all new programmers. You could read in any value with just the extraction operator and print anything, and in the background, the stream took care of the type of the input/output.

This is a safer and faster method than the old one existing inside the scanf, as you could no longer cause inconsistency issues between the values you passed as an argument for printing and what you specified to be inside the formatting string. For that reason, the question came up as to why you should create a new file to benefit from this.

A stream created inside the memory poll could assure that you can use all of that with some improved performance as compared to a proper file on the disk. The result of this is now known inside the C++ language as the stringstream class.

Using It for Input:

HTML clipboard

You will find the class defined/declared inside the stringstream header; thus, to use it, first you need to include the sstream, while adding the "using namespace std;" line will surely make it more enjoyable to write by letting you write less and produce more. There are three types of stringstream. First, we have one used only for output, one used with only input, and one combining the "muscles" of both input and output. Declare them as follows:

ostringstream outMemStream; // output

istringstream inMemStream; // input

stringstream memoryStream; // both in and out

If UNICODE is your daily bread, then you only need to append a "w" char to the beginning (prefix) to form the wide character counterparts of the upper ones, namely the wostringstream (for out), wistreangstream (for in) and wstringstream (for both).

Now let us look for some way to use this. Suppose you want to read a whole line of integers, and once that is done, calculate their sum. Reading in one member after the other is not a good solution, as the cin will stop for each white space (including the new line as well).

If we are talking about line reading, the most useful function remains the getline, so we should call it for help somehow. We may just read in a line, but how will we extract the numbers from it? This is where the stringstream comes in.

Each stringstream is in fact a stream if, resulting in a couple of advantages. For instance, you can take a stringstream and open it only for output. Moreover, there is another definition of the constructor that allows you to pass on a string as the first argument; this will fill the content of the string, the buffer.

This is perfect for us. Just get the input and put it inside the stream; after that, read it out of the stream one by one and total it. This gives us something close to this:

#include <iostream> // for accessing the console

#include <sstream>

#include <string>

using namespace std;

int main()

{

string input;

getline(cin, input);

stringstream memString(input, ios_base::in);

// == istringstream memString(input, ios_base::in);

 double summ = 0;

double temp;

 while (memString >> temp)

{

summ+= temp;

}

cout<< summ << endl;

return 1;

}

In fact, once you construct the stream it will behave just like a file stream, with the exception that this is automatically constructed inside the memory poll. It's a perfect solution for when you need a temporary container/buffer in which to store your data, and you can also extract it from there later on.

No wonder the program works flawlessly:

HTML clipboard

HTML clipboard

 1.234 2.48 3.206

6.92

Press any key to continue . . .

HTML clipboard For Output:

HTML clipboard

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

HTML clipboardFree Automatic Conversion:

HTML clipboard

We've arrived at the part we've been waiting for: the conversion. C++ performs implicit conversion with some basic types, such as long to double and signed to unsigned or base* to derived* (pointer cast); however, it requires explicit conversion in the case of transforming a string to a string/sequence of characters.

There have been countless functions made just for this task, and even more once the wide character made its place in the world. The usage of this is somewhat worrying, as it offers no buffer overflow protection (you're in big trouble if the allocated space is too little and you are wasting memory if you are exaggerating). The type safety is also weak, because you could just define one in the format string and give another type.

Using this eventually leads to a source of constant bug possibility inside your application. All the while you are also offering a security hole, as a foreign program could sense the volatile property of the buffer and start to "play with it." Generally, it is a good idea to avoid the stdio library and all of its functions; use instead the sstream.

If you were paying close attention during the previous pages, you can figure out the solution on your own, but let me explain it nevertheless. The buzz is that stringstreams perform automatic low-level conversion while inserting a new value.

The place at which this is most probable is inside a char sequence. The opposite occurs once you extract a new value from the stream (consequently converting the text into what it can -- number, double, int, etcetera, or throwing an error by setting the fail flag if this is possible for text to number). If you are interested in this, just flip the extraction lines in the previous page as follows:

memoryStream >> text;

memoryStream >> dnubmer;

memoryStream >> number;

Moreover, observe that the int has been cast into double and the double into int:

alfa 10 10.122

Extraction:

10

alfa

10

Press any key to continue . . .

The stream will allocate for it as much memory as it needs, so feel free to add to it; it will destroy the object once that is released/destroyed. In order to prove this part of the article with examples, I decided to write a little cast function with templates that turned out like this (see below).

#include <iostream> // for accessing the console

#include <sstream>

#include <string>

using namespace std;

template<typename castTo, typename from>

castTo cast( from item)

{

stringstream temp;

castTo result; //call default constructor to initialize

temp << item;

temp >> result;

return result;

};

int main()

{

string text;

double number = 2.234;

cout <<setprecision(3) << number

<< " is not "<< cast<string>(number) << endl;

return 1;

}

The print part looks like this:

2.23 is not 2.234

 




























HTML clipb<title>HTML clipboardVariety 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.

Many beginner coders, in fact, struggle with the conversions from one type to another. Probably the most troublesome problem turns out to be adding numbers of type integer and float to the std class string.

If you've always cursed this process, I invite you to continue reading this article. Even if you haven't, sit back, relax, and observe a solution that is simpler to write and maintain than the ones you may have used inside C (itoa, atoi, and others) until now.

This article is the ninth part of a series about streams in C++. Therefore, if you lack some general knowledge about this or the STL class string, I strongly encourage you to search for and read one or more of my articles about this published here on the Developer Shed Network under the names: Introduction to Streams, Basic IO in C++ or The String STL Class.

Today we will see how we can combine the power of the stream and the advantages of the STL class string together to make conversion of objects inside the C++ a seamless and trivial task.

As you may already recognize the benefits of streams as an easy way to read and write data with just the use of the insertion and extraction operator (<< and >>), the profit from strings is obvious: they offer an array of chars that does not need buffer management.

Working with streams to handle files has proven to be an efficient and comprehensible approach for all new programmers. You could read in any value with just the extraction operator and print anything, and in the background, the stream took care of the type of the input/output.

This is a safer and faster method than the old one existing inside the scanf, as you could no longer cause inconsistency issues between the values you passed as an argument for printing and what you specified to be inside the formatting string. For that reason, the question came up as to why you should create a new file to benefit from this.

A stream created inside the memory poll could assure that you can use all of that with some improved performance as compared to a proper file on the disk. The result of this is now known inside the C++ language as the stringstream class.

Using It for Input






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.