Overview
The CRT provides printf() variants to emit formatted output of data such as integers, boolean, strings etc. on different targets such as console, character buffers, files.
Similarly, CRT also provides scanf() variants to extract input from console, character buffers, files for data such as integers, boolean, strings etc.
This is demonstrated in this example.
The standard library Input and Output Stream classes provides an alternative for console, disk and string IO instead of CRT functions.
Details
IO Stream classes provides the same functionality in a structured and object oriented way. It uses manipulators to achieve formatting. Overloaded operator >>() and operator <<() along with dedicated functions to do the Input and Output. Additional functionalities that is not possible using CRT can also be achieved. Examples: custom formatting manipulators or I/O of any user defined or standard types.
Just like CRT, IO Stream classes can be used for IO with console, file and strings in memory.
This is demonstrated in this example 2.
A locale different from the current locale can be injected into IO stream classes to encode/decode independently while doing I/O.
Class Structure
The following depicts the class structure. Note that classes that start with the letter w are defined for whcar_t and other classes are defined for char. char16_t and char32_t are not supported.
The classes can be grouped into following categories - Primitive base, Intermediate base , disk based streams and String based streams.
Primitive Base classes
The following describes each of the bases classes in detail.
Input and Output Preparation
ios_base class describes the storage and member functions common to both input and output streams that don't depend on the template parameters.
basic_ios class describes the storage and member functions common to both input and output streams that don't depend on the template parameters.
Serialization
These base classes support read / write stream operations.
basic_streambuf class describes the mechanism of of the low level layer of reading and writing streams to devices like console, file and string.
Intermediate Base classes
These define intermediate base classes. It also includes console I/O stream objects.
Input and Output Preparation
basic_istream is the base class of all the input classes designed to receive inputs from various sources such as console, files and strings.
basic_ostream is the base class of all the output classes designed to publish outputs to various targets such as console, files and strings.
basic_iostream is the base class of all the classes that perform input and output operations on file and string streams.
Predefined Console IO Streams
These predefined console based streams are derived from istream and ostream.
The CRT provides three predefined file objects stdin mapped to console input; stdout and stderr mapped to console output.
Similarly, IO Stream classes also defines 2 sets (char_t and wchar_t) of 8 predefined streams as below.
Name | class | CRT | Description |
---|---|---|---|
cin | istream | stdin | Buffered Console input |
wcin | wistream | stdin | Buffered Console input |
cout | ostream | stdout | Buffered Console output |
wcout | wostream | stdout | Buffered Console output |
cerr | ostream | stderr | Buffered Console output |
wcerr | wostream | stderr | Buffered Console output |
clog | ostream | stderr | Unbuffered Console output |
wclog | wostream | stderr | Unbuffered Console output |
IO Stream extraction and insertion operators and Manipulators➹
istreams defines extraction operators that are basically overloaded operator >>() functions to extract values from the inputs
Similarly, ostreams defines insertion operators that are basically overloaded operator<<() functions to insert values into the output.
Manipulators are helper functions that prepare the input/output streams before using operator << or operator >>.
Disk based Streams
These provides stream classes that can be used Input only, Output only or Both with files.
Classes that does input only are derived from basic_istream. Similarly classes that does output only are derived from basic_ostream. Classes that do both input and output are derived from basic_iostream class.
Input and Output Preparation
basic_ifstream class is designed to receive inputs from sources such as file system.
basic_fstream class is designed to receive inputs from and send outputs to sources such as file system.
Serialization
basic_filebuf is a template based class used for read from or/and write to files in the file system. It's used by ifstream, ofstream and fstream classes to read from or/and write to files.
The example 8 demonstrates its usage.
String based streams
These provides stream classes that can be used Input only, Output only or Both with string.
Classes that does input only are derived from basic_istream. Similarly classes that does output only are derived from basic_ostream. Classes that do both input and output are derived from basic_iostream class.
Input and Output Preparation
basic_istringstream class is designed to receive inputs from sources such as strings in the memory.
basic_ostringstream class is designed to send outputs to sources such as strings in the memory.
basic_stringstream class is designed to receive inputs from and send outputs to sources such as strings in the memory.
The example 9 demonstrates its usage.
Stream iterators
istream_iterator and ostream_iterator can be used for typed input and output. They are faster as they skip construction of sentry objects.
istream iterators are input iterators that read successive elements from an input stream (such as cin).
The example 14 demonstrates its usage.
ostream iterators are output iterators that write successive elements from an output stream (such as cout).
Streambuf iterators
streambuf defines input iterator and output iterator for untyped input and output.
istreambuf_iterator and ostreambuf_iterator can be used instead of raw i/o apis.
istreambuf iterators are input iterators that read successive characters from an input stream (such as cin).
ostreambuf_iterator➹
ostreambuf iterators are output iterators that write successive characters from an output stream (such as cout).
The example 15 demonstrates its usage.
Synchronizations and Redirection
tie()
An input stream and output stream can be tied such that output stream is always flushed before input stream is fetched for input.
The example 10 demonstrates use of tie() operation as seen in its console output.
Here, EOF is received when input is fetched before tie(). After tie() the output is flushed and same is fetched during input.
Redirection of stream
Two streams can also be made to reuse same streambuf by instantiating the second stream with the streambuf of the first.
The example 11 demonstrates reuse of same streambuf to print same number in hex and decimal as seen in its console output.
Streambuf reuse
The contents of a predefined stream such as clog can be redirected to another stream such as a file.
Redirection using streambuf
Contents of a stream can also be outputted to cout.
This example 13 populating a istringstream with some text and later print it on console using its streambuf as seen its console output.
Summary of Examples
Name | Category | Description | Github | WandBox |
---|---|---|---|---|
Example | CRT Demo | stdin,stdout | source output | source + output |
Example 2 | IO Stream Demo | cin,cout | source output | source + output |
Example 3 | Custom Stream IO operator | operator >>, operator << | source output | source + output |
Example 4 | Stream Manipulators | ostream | source output | source + output |
Example 5 | Stream Manipulators | istream | source output | source + output |
Example 6 | Stream Manipulators | custom beeper | source output | source + output |
Example 7 | Stream State management and error handling | IO Flags | source output | source + output |
Example 8 | Disk IO | ifstream, ofstream | source output | source + output |
Example 9 | Memory IO | istringstream, ostringstream | source output | source + output |
Example 10 | Stream Iterator | istream_iterator, ostream_iterator | source output | source + output |
Example 11 | Streambuf Iterator | istreambuf_iterator, ostreambuf_iterator | source output | source + output |
Example 10 | Stream Synchronizations and Redirection | tie | source output | source + output |
Example 13 | Stream Synchronizations and Redirection | StreamBuf reuse | source output | source + output |
Example 14 | Stream Synchronizations and Redirection | redirection to clog | source output | source + output |
Example 15 | Stream Synchronizations and Redirection | redirection to cout | source output | source + output |
No comments:
Post a Comment