In C language, the input and output operations are performed using built-in library functions such as printf() and scanf(). But, C++ uses a new method of standard input and output called I/O stream.
A stream is a bytes of characters from memory to display on screen or read from the keyboard to the computer memory. The C++ language uses
cin to read inputs from keyboard and
cout to display characters on a screen.
The I/O Stream Library
The C++ standard library is a repository for a C++ program to get the desired functions. The I/O stream library is stored in following header files. These header files are specific to compilers.
Turbo C++ And Borland C++ IO Stream Files
Some other compilers uses extra stream files such as:
The stream library is nothing but a hierarchy of classes and functions for each class is defined. You will learn more about class in future lessons.
ios class It is the base class for input and output functions, other classes are derived from this class.
istream class This class is derived from ios class for input functions.
ostream class This class is derived from ios class for output functions.
iostream class Contains standard input and output function to like stdin and stdout in C programming language. The C++ uses
coutstream objects to perform standard input and output operations ( keyboard or screen output).
iomanip class This class contains functions for formatting the output stream. You will learn more about manipulator functions in next lesson.
C++ I/O Stream Objects
The I/O stream objects are used to perform the input and output operations. They take arguments such as objects, variables, strings, and so on. Basically, all are characters to the stream so there is no conflict with respect to data type.
In C language, every input and output operation has a format specifier %d, %s, without which that input or output operation will not work. This is not a requirement in C++ programming language due to iostream.
The C++ uses following objects for input and output.
The cout object performs the output operation with the help of extraction operator(<<) to a standard output device (e.g computer screen). The syntax for cout is:
cout << var 1 << var 2 << ...<< var n;
There are many ways to write output statement in C++.
cout << number << number 2 << number3 ; cout << "Number =" << number ; cout << number ; cout << number2;
The cout is associated with c standard output
The cin object used to read the input values with the help of insertion operator (>>) from a standard input device such as keyboard.
The syntax for cin is:
cin >> var 1 >> var 2 >> var n;
The cin examples are as follows.
cin >> number; cin >> A >> B >> C;
The cin is associated with
It is used for reporting error and it has its unitbuf flag set which means that the error stream is flushed every time. This flush is performed to write the error into an external device quickly using stderr (standard C I/O).
The clog is slower and not written to the external device quickly, unless the buffer is full. It is also associated with stderr files.
Summary of IO Streams
|IO stream||Meaning||Associated files|
|cerr||standard error reporting||stderr|