C++ Basic Input/Output

In this tutorial you will learn about the C++ Basic Input/Output and its application with practical example.

C++ Basic Input/Output

Input/Output operation is the way general purpose computer can communicate or interact to the outside world. In C++, I/O occurs in form of a data streams, which basically nothing other than a sequences of bytes. When bytes flow from a input device such as a keyboard, mouse, disk drive, or from a network to main memory, then this sequence of bytes is called as input stream and operation is called as input operation and if bytes flow from main memory to a output device like such as computer screen, a printer, a disk drive, or to a network , then this sequence of bytes is called as output stream and this operation is called as output operation. The C++, comes with an rich set of standard libraries for input/output operations. To perform any of the standard input and output operation we must have to include respective header files in our program.

In C++, we have following header files important for basic I/O operation –

Header File Function and Description
<iostream> It is used to define the cout, cin and cerr objects, which correspond to standard output stream, standard input stream and standard error stream, respectively.
<iomanip> It is used to declare services useful for performing formatted I/O, such as setprecision and setw.
<fstream> It is used to declare services for user-controlled file processing.

There are following standard streams are available to all c++ programs –

  • cin (standard input)
  • cout (standard output)
  • cerr (standard error)
  • clog (standard log)

C++ Standard Output Stream (cout)

The cout is basically a predefined object of ostream class. The cout object in conjunction with stream insertion operator (<<) allows us to send data to standard output device(screen).

Example:-

Output:-

C++ Standard Input Stream (cin)

The cin is basically a predefined object of istream class. The cin object in conjunction with stream extraction operator (>>) allows us to accept/extract data from standard input device(keyboard).

Example:-

Output:-

C++ Standard Error Stream (cerr)

The cerr is also a predefined object of ostream class. The cerr object in conjunction with stream insertion operator (<<) allows us to send un-buffered error messages to standard error device(screen).

Example:-

Output:-

C++ Standard Log Stream (clog)

The clog is also a predefined object of ostream class. The clog object in conjunction with stream insertion operator (<<) allows us to send buffered error messages to standard error device(screen) until the buffer is filled or until the buffer is flushed.

Example:-

Output:-

 

 

In this tutorial we have learn about the C++ Basic Input/Output and its application with practical example. I hope you will like this tutorial.