Managing Console I/O operations in C++ - GeeksforGeeks

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.
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.
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).
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;}
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
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
It inherits the properties of ios
It declares input functions such as get(), getline(), and read()
It contains overload extraction operator >>
It inherits the properties of ios.
It declares input functions such as put() and write().
It contains an overload extraction operator <<.
Inherits the properties of ios stream and ostream through multiple inheritances and thus contains all the input and output functions.
It provides an interface to physical devices through buffers.
It acts as a base for filebuf class used ios file.
CPP-Basics
cpp-input-output
Input and Output
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Header files in C/C++ and its uses
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Sorting a Map by value in C++ STL