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.

Advertisements

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

– It is the base class for input and output functions, other classes are derived from this class.

– This class is derived from ios class for input functions.

– This class is derived from ios class for output functions.

– Contains standard input and output function to like and in C programming language. The C++ uses and objects to perform standard input and output operations ( keyboard or screen output).

– 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 – , , 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
Advertisements

cout

The object performs the output operation with the help of extraction operator() to a standard output device (e.g computer screen). The syntax for 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 is associated with c standard output – .

cin

The object used to read the input values with the help of insertion operator () from a standard input device such as keyboard.

The syntax for is:

cin >> var 1 >> var 2 >> var n;

The examples are as follows.

cin >> number;
cin >>  A >> B >> C;

The is associated with files.

cerr

It is used for reporting error and it has its 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 files.

Summary of IO Streams

IO streamMeaningAssociated files
coutstandard outputstdout
cinstandard inputstdin
cerrstandard error reportingstderr
clogstandard logstderr
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version