Basic Input/output in C++

C++ comes with libraries that help in performing basic input/output operations. In c++, a sequence of bytes is corresponding to input/output is called a stream.

Rows of data from input devices(keyboard, mouse) to main memory are called input streams.

Rows of data from main memory to output devices(monitor, printer) are called output streams.

Source: Unsplash
  • iostream- Header that defines standard input/output. It provides the functions cin, cout, cerr, clog. cin is the standard input function. cout is the standard output function. cerr is the standard output stream for errors. clog is the standard output stream for logging.
  • iomanip- Header that provides parametric manipulators. It contains functions that affect the state of iostream objects.
  • fstream- fstream contains functions to create files, write information to files, and read the contents of the files. (More on this part will be covered in detail in the later part of the series).
  • cin and cout —

Here we display the messages to enter the numbers using cout. The input of numbers is taken using cin function. The sum of numbers is directly calculated and printed. Note that we can join multiple outputs by trailing << also known as Insertion operator. The >> is called an extraction operator.

  • cerr —

cerr is used along with an insertion operator to display a string of characters usually error messages. Any messages sent to cerr are immediately flushed to the OS as cerr is an unbuffered function.

  • clog —

clog is used to log messages as clog is a buffered function. A buffered function is usually more performance efficient than an unbuffered function as it keeps storing the logs in the buffer and writes them on the disk all at once.

I hope this gave you a brief idea about different io functions. If you didn’t get the technical details of any function. Don’t worry about it.

I am a CS graduate and an ML enthusiast. My purpose here is to guide beginners in their programming and Machine learning journey.