The C language did not build the input/output facilities into the language. In other words, there is no keyword like read
or write
. Instead, it left the IO to the compiler as external library functions [such as printf
and scanf
in stdio
library]. The ANSI C standard formalized these IO functions into Standard IO package [stdio.h
]. C++ continues this approach and formalizes IO in libraries such as iostream
and fstream
.
- C++ IO is type safe. IO operations are defined for each of the type. If IO operations are not defined for a particular type, compiler will generate an error.
- C++ IO operations are based on streams of bytes and are device independent. The same set of operations can be applied to different types of IO devices.
Stream IO
Streams
C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs [just like water and oil flowing through a pipe]. In input operations, data bytes flow from an input source [such as keyboard, file, network or another program] into the program. In output operations, data bytes flow from the program to an output sink [such as console, file, network or another program]. Streams acts as an intermediaries between the programs and the actual IO devices, in such the way that frees the programmers from handling the actual devices, so as to archive device independent IO operations.
C++ provides both the formatted and unformatted IO functions. In formatted or high-level IO, bytes are grouped and converted to types such as int
, double
, string or user-defined types. In unformatted or low-level IO, bytes are treated as raw bytes and unconverted. Formatted IO operations are supported via overloading the stream insertion
[] operators, which presents a consistent public IO interface.
To perform input and output, a C++ program:
- Construct a stream object.
- Connect [Associate] the stream object to an actual IO device [e.g., keyboard, console, file, network, another program].
- Perform input/output operations on the stream, via the functions defined in the stream's pubic interface in a device independent manner. Some functions convert the data between the external format and internal format [formatted IO]; while other does not [unformatted or binary IO].
- Disconnect [Dissociate] the stream to the actual IO device [e.g., close the file].
- Free the stream object.
C++ IO Headers, Templates and Classes
HeadersC++ IO is provided in headers [which included
,
,
and
],
[for file IO], and
[for string IO]. Furthermore, the header
provided manipulators such as
setw[]
, setprecision[]
setfill[]
and setbase[]
for formatting.
In order to support various character sets [char
and wchar_t
in C++98/03; and char16_t
, char32_t
introduced in C++11], the stream classes are written as template classes, which could be instantiated with an actual character type. Most of
the template classes take two type parameters. For example,
template class basic_istream; template class basic_ostream;
where:
charT
is the character type, such aschar
orwchar_t
;traits
, of another template classchar_traits
, defined the properties of the character operations such as the collating sequence [sorting order] of character set.
As mention, the basic_xxx
template classes can be instantiated with a character type, such as char
and
wchar_t
. C++ further provides typedef
statements to name these classes:
typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;Specialization Classes for char type
We shall focus on the specialization classes for char
type:
ios_base
andios
: superclasses to maintain common stream properties such as format flag, field width, precision and locale. The superclassios_base
[which is not a template class] maintains data that is independent of the template parameters; whereas the subclassios
[instantiation of templatebasic_ios
] maintains data which is dependent of the template parameters.istream
[basic_istream
],ostream
[basic_ostream
]: provide the input and output public interfaces.iostream
[basic_iostream
]: subclass of bothistream
andostream
, which supports bidirectional input and output operations. Take note thatistream
andostream
are unidirectional streams; whereasiostream
is bidirectional.basic_iostream
template andiostream
class is declared in theheader, not
header.
ifstream
,ofstream
andfstream
: for file input, output and bidirectional input/output.istringstream
,ostringstream
andstringstream
: forstring
buffer input, output and bidirectional input/output.streambuf
,filebuf
andstringbuf
: provide memory buffer for the stream, file-stream and string-stream, and the public interface for accessing and managing the buffer.
Buffered IO
[TODO]
The Header and the Standard Stream Objects: cin, cout, cerr and clog
The header also included the these headers:
,
,
and
. Hence, your program needs to include only the
header for IO operations.
The header declares these standard stream objects:
cin
[ofistream
class,basic_istream
specialization],wcin
[ofwistream
class,basic_istream
specialization]: corresponding to the standard input stream, defaulted to keyword.cout
[ofostream
class],wcout
[ofwostream
class]: corresponding to the standard output stream, defaulted to the display console.cerr
[ofostream
class],wcerr
[ofwostream
class]: corresponding to the standard error stream, defaulted to the display console.clog
[ofostream
class],wclog
[ofwostream
class]: corresponding to the standard log stream, defaulted to the display console.
The Stream Insertion Operators
Formatted output is carried out on streams via the stream insertion operators. For example,
cout > variable;
Take note that cin
/cout
shall be the left operand and the data flow in the direction of the arrows.
The operators are overloaded to handle fundamental types [such as
int
and
double
], and classes [such as string
]. You can also overload these operators for your own user-defined types.
The cin
return a reference to cin
and cout
, and thus, support cascading operations. For example,
cout