Managing Console I/O operations in C++
Last Updated :
07 Apr, 2021
Every program takes some data as input and generates processed data as an output following the familiar input process output cycle. It is essential to know how to provide the input data and present the results in the desired form. The use of the cin and cout is already known with the operator >> and << for the input and output operations. In this article, we will discuss how to control the way the output is printed.
C++ supports a rich set of I/O functions and operations. These functions use the advanced features of C++ such as classes, derived classes, and virtual functions. It also supports all of C's set of I/O functions and therefore can be used in C++ programs, but their use should be restrained due to two reasons.
- I/O methods in C++ support the concept of OOPs.
- I/O methods in C cannot handle the user-define data type such as class and object.
It uses the concept of stream and stream classes to implement its I/O operations with the console and disk files.
C++ Stream
The I/O system in C++ is 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 the stream.
- A stream is a sequence of bytes.
- The source stream that provides the data to the program is called the input stream.
- The destination stream that receives output from the program is called the output stream.
- The data in the input stream can come from the keyboard or any other input device.
- The data in the output stream can go to the screen or any other output device.
C++ contains several pre-defined streams that are automatically opened when a program begins its execution. These include cin and cout. It is known that cin represents the input stream connected to the standard input device (usually the keyboard) and cout represents the output stream connected to the standard output device (usually the screen).
C++ Stream Classes
The C++ I/O system contains a hierarchy of classes that are used to define various streams to deal with both the console and disk files. These classes are called stream classes. The hierarchy of stream classes used for input and output operations is with the console unit. These classes are declared in the header file iostream. This file should be included in all the programs that communicate with the console unit.
C++
#include <iostream>
using namespace std;
int main()
{
cout << " This is my first"
" Blog on the gfg";
return 0;
}
Output:
This is my first Blog on the gfg
The ios are the base class for istream (input stream) and ostream (output stream) which are in turn base classes for iostream (input/output stream). The class ios are declared as the virtual base class so that only one copy of its members is inherited by the iostream.
The class ios provide the basics support for formatted and unformatted I/O operations. The class istream provides the facilities formatted and unformatted input while the class ostream (through inheritance) provides the facilities for formatted output.
The class iostream provides the facilities for handling both input and output streams. Three classes add an assignment to these classes:
- istream_withassign
- ostream_withassign
- iostream_withassign
Tabular Representation of stream classes for Console Operation:
Class name | Content |
---|
ios (General input/output stream class) | - Contains basic facilities that are used by all other input and output classes
- Also contains a pointer to a buffer object (streambuf object)
- Declares Constants and functions that are necessary for handling formatted input and output operations
|
istream (Input stream) | - It inherits the properties of ios
- It declares input functions such as get(), getline(), and read()
- It contains overload extraction operator >>
|
ostream (Output Stream) | |
iostream (Input/output stream) | - Inherits the properties of ios stream and ostream through multiple inheritances and thus contains all the input and output functions.
|
Streambuf | - It provides an interface to physical devices through buffers.
- It acts as a base for filebuf class used ios file.
|
Similar Reads
Overloading of function-call operator in C++ In this article, we will discuss the Overloading of the function-call operators in C++. The function call operator is denoted by â()â which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.When the function call operator is overlo
3 min read
How to Open and Close a File in C++? In C++, we can open a file to perform read and write operations and close it when we are done. This is done with the help of fstream objects that create a stream to the file for input and output. In this article, we will learn how to open and close a file in C++. Open and Close a File in C++ The fst
2 min read
std::ifstream::is_open() in C++ The std::ifstream::is_open() function in C++ is a member function of the std::ifstream class, which is used to check if a file stream object has successfully opened a file for reading or not. This function returns a boolean value, true if the file stream is open and ready for I/O operations, and fal
2 min read
C++ Program to Copy One File into Another File To copy the text/contents of one file to another file, we should know the basics of reading and writing a text file in C++. To copy the file using C++, we read the contents of the source file and write it into the destination file. Steps to copy one file to another in C++: Create objects of ifstream
2 min read
Unformatted input/output operations In C++ In this article, we will discuss the unformatted Input/Output operations In C++. Using objects cin and cout for the input and the output of data of various types is possible because of overloading of operator >> and << to recognize all the basic C++ types. The operator >> is overlo
4 min read
Dynamic initialization of object in C++ In this article, we will discuss the Dynamic initialization of objects using Dynamic Constructors. Dynamic initialization of object refers to initializing the objects at a run time i.e., the initial value of an object is provided during run time.It can be achieved by using constructors and by passin
4 min read