Javatpoint Logo
Javatpoint Logo

C++ Basic Input and Output (I/O)

C++ I/O operation uses the stream concept. The series of bytes or flow of data is referred to as a stream. It accelerates performance.

If bytes are transferred from main memory to a device like a printer, display screen, network connection, etc. this is called an output operation.

An input operation occurs when bytes flow from a device such as a printer, display screen, or network connection to main memory.

In C++, predefined functions and declarations are provided through header files, allowing you to do specific tasks without having to write new code from the start. A few important header files for input/output operations in C++ include functions for effectively carrying out input and output tasks. The C++ Standard Library, a collection of classes and methods created in the C++ programming language, contains these header files. Let us discuss the main header files for input/output operations:

Header File Function and Description
<iostream> It is used to define the cout, cin and cerr objects, which correspond to standard output stream, standard input stream and standard error stream, respectively.
<iomanip> It is used to declare services useful for performing formatted I/O, such as setprecision and setw.
<fstream> It is used to declare services for user-controlled file processing.

iostream: It is one of the most important header files for input/output operations in C++. It stands for "input-output" stream. For working with various forms of input/output streams, the iostream header file includes the classes istream (input stream) and ostream (output stream) as well as its derived classes ifstream, ofstream, and stringstream. This header file's most typically used classes are cin (standard input) and cout (standard output), which allow you to read user input and display output to the console. For example:

Output

Enter a number: 42
You entered: 42

iomanip: This header file stands for "input-output manipulation". It provides tools to format input and output. It allows you to modify the alignment, width, precision, and other formatting features of the input and output. Setw, setprecision, fixed, left, right, and other regularly used functions are listed below. It is especially handy for presenting data in a certain way.

Example:

Output

Value of pi: 3.14

fstream: The header file for file input/output operations is called fstream. It comprises classes for reading from and writing to files ifstream (input file stream) and ofstream (output file stream). The system uses these classes to open read-only and write-only files.

Example:

Output

The file was written successfully.

These header files are among the most crucial for C++ input/output tasks. Each one has a specific purpose and offers the tools necessary to successfully manage tasks involving input and output, whether it is interacting with the console, formatting output, or working with files.

In C++, we frequently use 'using namespace std;' after the header files. The namespace std; statement is frequently used in C++ to streamline the code when working with standard library components. Let's examine this statement's function and application in more detail:

A namespace is a technique for grouping similar identifiers (such as classes, functions, and variables) to prevent naming conflicts. The C++ Standard Library provides its parts (such as cin, cout, etc.) under the std namespace.

The term "standard" is shortened to "std", and all elements of the standard library are contained within it. By doing this, name conflicts with identifiers set up in your code are reduced.

Now let us talk about why the using namespace std; statement is used:

Without using namespace std:

As you can see, using the namespace std; statement allows you to omit the std:: prefix when accessing standard library components. It makes your code shorter and more readable, as you don't have to repeat std:: before each standard library identifier.

I/O Library Header Files

Standard output stream (cout):

The cout object is an ostream class predefined object. It is connected to the standard output device, which is usually a display screen. The cout is used in combination with the stream insertion operator (<<) to show the output on a console

Let us see the simple example of a standard output stream (cout):

Output

Value of ary is: Welcome to C++ tutorial

Standard input stream (cin)

The cin is a predefined object of istream class. It is connected with the standard input device, which is usually a keyboard. The cin is used in conjunction with stream extraction operator (>>) to read the input from a console.

Let's see the simple example of standard input stream (cin):

Output

Enter your age: 22
Your age is: 22

Standard end line (endl)

The endl is a predefined object of ostream class. It is used to insert a new line characters and flushes the stream.

Let's see the simple example of standard end line (endl):

Output

C++ Tutorial Javatpoint 
End of line

Un-buffered standard error stream (cerr):

cerr stands for "standard error".

It is an unbuffered stream, meaning that output sent to cerr is immediately displayed on the console without buffering.

It is typically used for displaying error messages and diagnostic information, which need to be displayed immediately to avoid delays caused by buffering.

Example: using cerr:

Output

This is an error message.

buffered standard error stream (clog):

clog stands for "standard log". It is a buffered stream, similar to cout. It's often used for writing informational or diagnostic messages that are less time-sensitive than errors. The use of buffering can improve performance when displaying a large number of messages.

Example: using clog

Output

This is an informational message.

In both examples, the output will appear on the console. However, the main difference between cerr and clog lies in their buffering behavior. Due to its unbuffered nature, messages given to cerr are displayed right away, but messages sent to clog may be buffered for greater speed. However, they will still eventually appear on the console.

Note: It is important to remember that the type of message you wish to display will determine whether you use cerr or clog. Use cerr for essential messages that need immediate attention (like error messages) and use clog for less critical diagnostic or informational messages that can be buffered for better performance.


Next TopicC++ Variables





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA