www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 902 (continued)]

18.12. String Stream Processing

In addition to standard stream I/O and file stream I/O, C++ stream I/O includes capabilities for inputting from, and outputting to, strings in memory. These capabilities often are referred to as in-memory I/O or string stream processing.

Input from a string is supported by class istringstream. Output to a string is supported by class ostringstream. The class names istringstream and ostringstream are actually aliases defined by the typedefs

  typedef basic_istringstream< char > istringstream;
  typedef basic_ostringstream< char > ostringstream;

Class templates basic_istringstream and basic_ostringstream provide the same functionality as classes istream and ostream plus other member functions specific to in-memory formatting. Programs that use in-memory formatting must include the <sstream> and <iostream> header files.

One application of these techniques is data validation. A program can read an entire line at a time from the input stream into a string. Next, a validation routine can scrutinize the contents of the string and correct (or repair) the data, if necessary. Then the program can proceed to input from the string, knowing that the input data is in the proper format.

Outputting to a string is a nice way to take advantage of the powerful output formatting capabilities of C++ streams. Data can be prepared in a string to mimic the edited screen format. That string could be written to a disk file to preserve the screen image.


[Page 903]

An ostringstream object uses a string object to store the output data. The str member function of class ostringstream returns a copy of that string.

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.

Figure 18.11. Using a dynamically allocated ostringstream object.
(This item is displayed on pages 903 - 904 in the print version)

 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


Lines 2728 output string string1, string string2, string string3, double double1, string string4, int integer, string string5 and the address of int integerall 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 2728. Line 34 demonstrates that more data can be appended to the string in memory by simply issuing another stream insertion operation to outputString. Lines 3536 display string outputString after appending additional characters.


[Page 904]

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.

Figure 18.12 demonstrates input from an istringstream object. Lines 1516 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


[Page 905]

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.

Figure 18.12. Demonstrating input from an istringstream object.
(This item is displayed on pages 904 - 905 in the print version)

 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


The data is then output in lines 2528. 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.


Previous Page
Next Page