cpp input output Introduction

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.

input output streams in c++

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.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise