Skip to content
Home » C++ I/O Stream

C++ I/O Stream

    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\slashO stream library is stored in following header files. These header files are specific to compilers.

    Turbo C++  And Borland C++ IO Stream Files

    • iostream.h
    • ios.h
    • iomanip.h

    Some other compilers uses extra stream files such as:

    • istream.h
    • ostream.h

    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 \hspace{3px} class – It is the base class for input and output functions, other classes are derived from this class.

    istream \hspace{3px} class – This class is derived from ios class for input functions.

    ostream \hspace{3px} class – This class is derived from ios class for output functions.

    iostream \hspace{3px} class – Contains standard input and output function to like stdin and stdout in C programming language. The C++ uses cin and coutstream objects to perform standard input and output operations ( keyboard or screen output).

    iomanip \hspace{3px} 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.

    • cout
    • cin
    • cerr
    • clog

    cout

    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++.

    For example,

    cout << number << number 2 << number3 ;
    cout << "Number =" << number ;
    cout << number ;
    cout << number2;

    The cout is associated with c standard output – stdout.

    cin

    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 stdin files.

    cerr

    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).

    clog

    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 streamMeaningAssociated files
    coutstandard outputstdout
    cinstandard inputstdin
    cerrstandard error reportingstderr
    clogstandard logstderr