MANAGING CONSOLE I/O OPERATIONS
Every program takes some data as inputs and generates processed data as output following the familiar
input-process-output (data) cycle. It’s, therefore, essential to know how to provide the input data and how to
present the results in a desired form. We have, in the previous chapters, used cin and cout with the operators << and << for the input and output operations. But we have not so far discussed as to how to control the way the output is printed.
C++ supports a rich set of I/O functions and operations to do this.
Since these functions use the advanced features of c++( such as classes, derived classes and virtual
functions), we need to know a lot about them before really implementing the c++ I/O operations.
Remember, c++ supports all of C’s rich set of I/O functions. We can use any of them in c++ programs. we can use any of them in the c++ programs. But we restricted from using them due to two reasons. First, I/O methods inn c++ support the concepts of OOP and secondly, I/O methods in c cannot handle the user-defined data types such as class objects.
C++ uses the concept of stream and stream classes to implement its I/O operations with the console
and disk files. We will discuss in this chapter, how stream classes support the console- oriented
input-output operations. File- oriented I/O operations will be discussed in the next chapter
C++ STREAMS
The I/O system in c++ designed to work with a wide variety of devices including terminals, disks, and tape drives. Although each device is very different, the I/O system supplies an interface to the programmer that is independent of the actual device being accessed. This interface is known as stream.
A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent. The source stream that provides data to the program is called the input stream and the destination stream that receives output from the program is called the output stream. In other words, a program extracts the bytes from an input stream and inserts bytes into an output stream as illustrated in Fig. 10.1.
The data to the input stream can come from the keyboard or any other storage device. Similarly, the data in the output stream can go to any storage devices or screen. As we see earlier, a stream acts as an interface between the program and the I/O device.
Therefore, a c++ programs handles data (input or output) independent of the devices used.
C++ language contains several pre-defined streams that are automatically opened when a program begins its execution. These include cin and cout which have been very often in our earlier programs. We know that
cin operator represents the input stream connected to the standard input device (usually the keyboard) and cout operator represents the output stream connected to the standard output device (usually the screen).
Note : the keyboard and the screen are default options. We can redirect streams to the other devices
or files, if necessary.