I/O Streams

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.
NameclassCRTDescription
cin istream stdinBuffered Console input
wcin wistream stdinBuffered Console input
cout ostream stdoutBuffered Console output
wcout wostream stdoutBuffered Console output
cerr ostream stderrBuffered Console output
wcerr wostream stderr Buffered Console output
clog ostream stderrUnbuffered Console output
wclog wostream stderrUnbuffered 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_ofstream class is designed to send outputs to 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.

Serialization

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_iterator➹
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_iterator➹
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.
This example 12 redirects output of clog to a file as seen in its console output.

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
NameCategoryDescriptionGithubWandBox
Example CRT Demostdin,stdoutsource  outputsource + output
Example 2  IO Stream Democin,coutsource  outputsource + output
Example 3Custom Stream IO operatoroperator >>, operator <<source  outputsource + output
Example 4Stream Manipulatorsostreamsource  outputsource + output
Example 5Stream Manipulatorsistreamsource  outputsource + output
Example 6Stream Manipulatorscustom beepersource  outputsource + output
Example 7Stream State management
and error handling
IO Flagssource  outputsource + output
Example 8Disk IOifstream, ofstreamsource  outputsource + output
Example 9Memory IOistringstream,
ostringstream
source  outputsource + output 
Example 10Stream Iteratoristream_iterator, ostream_iteratorsource  outputsource + output
Example 11Streambuf Iteratoristreambuf_iterator, ostreambuf_iteratorsource  outputsource + output
Example 10 Stream Synchronizations 
and Redirection
tiesource  outputsource + output
Example 13Stream Synchronizations 
and Redirection
StreamBuf reusesource  outputsource + output
Example 14Stream Synchronizations 
and Redirection
redirection to clogsource  outputsource + output
Example 15Stream Synchronizations 
and Redirection
redirection to coutsource  outputsource + output










No comments:

Post a Comment