In addition to standard stream I/O and file stream I/O, C++ stream I/O includes capabilities for inputting from, and outputting to, string
s 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 typedef
s
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>
headers.
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.
To assist with data validation, C++11 provides powerful regular-expression capabilities. For example, if a program requires a user to enter a U.S. format telephone number (e.g., (800) 555-1212
), you can use a regular-expression pattern to confirm that the user’s input matches the expected format. Many websites provide regular expressions for validating email addresses, URLs, phone numbers, addresses and other popular kinds of data. We introduce regular expressions and provide several examples in Chapter 24.
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.
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
.3
ostringstream
Figure 21.11 demonstrates an ostringstream
object. The program creates ostringstream
object outputString
(line 9) and uses the stream insertion operator to output a series of string
s and numerical values to the object.
Lines 21–22 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 25 uses the stream insertion operator and the call outputString.str()
to display a copy of the string
created in lines 21–22. Line 28 demonstrates that more data can be appended to the string
in memory by simply issuing another stream insertion operation to outputString
. Lines 29–30 display string outputString
after appending additional characters.
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.
istringstream
Figure 21.12 demonstrates input from an istringstream
object. Lines 9–10 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
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 17.
The data is then output in lines 19–22. The program attempts to read from inputString
again in line 26. The if
condition in line 29 uses function good
(Section 13.8) to test if any data remains. Because no data remains, the function returns false
and the else
part of the if
…else
statement executes.