Many applications input information from string objects or dynamically build string objects in memory. This is done primarily to increase application performance. For example, rather than read individual data items from a file, an application can read a chunk of data into a string object, then perform the individual data inputs with the more efficient "in-memory" input operations. Similarly, an application can build a string object in memory that contains many pieces of data, then output that string object in one operation to a file. C++'s string streams facilitate performing in-memory I/O with string objects. This tutorial is intended for students and professionals who are familiar with standard C++ I/O with cin and cout or with C++ file processing.
Download the code examples for this tutorial.
[Note: This tutorial is an excerpt (Section 18.12) of Chapter 18, Class string and String Stream Processing, from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.902-905. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
18.12 String Stream Processing (Continued)
Figure 18.11 demonstrates an ostringstream object. The program creates ostringstream object outputString (line 15) and uses the stream insertion operator to output a series of strings and numerical values to the object.
Lines 27–28 output string string1, string string2, string string3, double double1, string string4, int integer, string string5 and the address of int integer—all to outputString in memory. Line 31 uses the stream insertion operator and the call outputString.str() to display a copy of the string created in lines 27–28. Line 34 demonstrates that more data can be appended to the string in memory by simply issuing another stream insertion operation to outputString. Lines 35–36 display string outputString after appending additional characters.
1 // Fig. 18.11: Fig18_11.cpp 2 // Using a dynamically allocated ostringstream object. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include <string> 8 using std::string; 9 10 #include <sstream> // header file for string stream processing 11 using std::ostringstream; // stream insertion operators 12 13 int main() 14 { 15 ostringstream outputString; // create ostringstream instance 16 17 string string1( "Output of several data types " ); 18 string string2( "to an ostringstream object:" ); 19 string string3( "\n double: " ); 20 string string4( "\n int: " ); 21 string string5( "\naddress of int: " ); 22 23 double double1 = 123.4567; 24 int integer = 22; 25 26 // output strings, double and int to ostringstream outputString 27 outputString << string1 << string2 << string3 << double1 28 << string4 << integer << string5 << &integer; 29 30 // call str to obtain string contents of the ostringstream 31 cout << "outputString contains:\n" << outputString.str(); 32 33 // add additional characters and call str to output string 34 outputString << "\nmore characters added"; 35 cout << "\n\nafter additional stream insertions,\n" 36 << "outputString contains:\n" << outputString.str() << endl; 37 return 0; 38 } // end main |
outputString contains:
Output of several data types to an ostringstream object:
double: 123.457
int: 22
address of int: 0012F540
after additional stream insertions,
outputString contains:
Output of several data types to an ostringstream object:
double: 123.457
int: 22
address of int: 0012F540
more characters added
|
Fig. 18.11 Using a dynamically allocated ostringstream object.
An istringstream object inputs data from a string in memory to program variables. Data is stored in an istringstream object as characters. Input from the istringstream object works identically to input from any file. The end of the string is interpreted by the istringstream object as end-of-file.

