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.12 demonstrates input from an istringstream object. Lines 15–16 create string input containing the data and istringstream object inputString constructed to contain the data in string input. The string input contains the data
Input test 123 4.7 A
1 // Fig. 18.12: Fig18_12.cpp 2 // Demonstrating input from an istringstream object. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include <string> 8 using std::string; 9 10 #include <sstream> 11 using std::istringstream; 12 13 int main() 14 { 15 string input( "Input test 123 4.7 A" ); 16 istringstream inputString( input ); 17 string string1; 18 string string2; 19 int integer; 20 double double1; 21 char character; 22 23 inputString >> string1 >> string2 >> integer >> double1 >> character; 24 25 cout << "The following items were extracted\n" 26 << "from the istringstream object:" << "\nstring: " << string1 27 << "\nstring: " << string2 << "\n int: " << integer 28 << "\ndouble: " << double1 << "\n char: " << character; 29 30 // attempt to read from empty stream 31 long value; 32 inputString >> value; 33 34 // test stream results 35 if ( inputString.good() ) 36 cout << "\n\nlong value is: " << value << endl; 37 else 38 cout << "\n\ninputString is empty" << endl; 39 40 return 0; 41 } // end main |
The following items were extracted from the istringstream object: string: Input string: test int: 123 double: 4.7 char: A inputString is empty |
Fig. 18.12 Demonstrating input from an istringstream object.
which, when read as input to the program, consist of two strings ("Input" and "test"), an int (123), a double (4.7) and a char ('A'). These characters are extracted to variables string1, string2, integer, double1 and character in line 23.
The data is then output in lines 25–28. The program attempts to read from inputString again in line 32. The if condition in line 35 uses function good (Section 15.8) to test if any data remains. Because no data remains, the function returns false and the else part of the if...else statement is executed.

