Basic Input/Output Operations In C++

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

A Comprehensive Study Of Input/Output Operations In C++.

In this tutorial, we will discuss C++ input/output (I/O) operations in detail.

Data is transferred to/from output/input device in the form of a sequence of bytes called stream. The stream flowing from an input device like a keyboard to the main memory, it is called the Input Operation.

On the other hand, streams that flow from the main memory to an output device like a screen is called an Output Operation.

=> Check Out The Best C++ Training Tutorials Here.

IO OPERATIONS IN C++

C++ provides us with an extensive set of I/O functions through its libraries.

C++ I/O Library Header Files

C++ provides the following I/O header files:

Header filedescription
iostreamiostream defines following objects
cout-> standard output
cin - > standard input
clog – standard log (buffered)
cerr – standard error(un-buffered)
iomanipiomanip defines parameterized stream manipulators like setw and setprecision which aids in formatting I/O . We will discuss this topic along with file I/O.
fstreamfstream helps in I/O processing of files which we will discuss in more details in the topic “File I/O”.

Standard Output Stream (cout)

C++ standard output stream – cout is an object of the ostream class which has iostream as its parent. Cout is used in with the operator “<<” and is also called as an insertion operator to output the information or data to an output device. The display screen is usually the output device to which the cout object is connected to.

Depending on the data types used, C++ compiler determines the data displayed and also determines the type of insertion operator to be used for displaying the data.

The object Cout and the insertion operator support the built-in data types of C++, string and pointer values.

We can also use more than one insertion operator along with cout in a single statement.

For Example,

cout<<” Hello, World!!”<<” Good morning!!”;

When “endl” is being used at the end of cout, it indicates the next line.

Standard Input Stream (cin)

C++ standard input stream – cin is an object of class istream class which is also a child of iostream class. The cin object along with “>>”, which is also known as extraction operator is used to read data from the input device. An Example of an input device to which cin is connected to is a keyboard.

As per the data type, C++ compiler determines the data to be read and also determines the type of extraction operator to be used for reading and storing data. Just like cout, we can use more than one extraction operator in a single cin statement.

When “endl” is used at the end of the cin statement, it indicates the end of the line.

In the Example given below, we demonstrate the usage of cin and cout in C++.

#include <iostream>
using namespace std;

int main( )
{
    char str[] = "This is C++ basic Input Output";
    int number;
    cout<<"Enter the number: "; cin>>number;
    cout<<"The number entered is: "<<number<<endl;
    cout << "Value of str is: " << str << endl;
}

Output:

Enter the number: 100
The number entered is: 100
Value of str is: This is C++ basic Input Output

As we see in the above program, we use cin to read a number from the keyboard and store it in an integer variable named “number”. Then using cout, we display this number and also the character message.

Standard Error (cerr) And Standard Log (clog) Streams

Both cerr and clog are objects of the ostream class which are similar to cout and cin. Clog and cerr are used for writing log and error messages respectively to standard log or error devices which can also be a display screen. Though both are the members of stderr (standard error), the main difference between clog and cerr is that clog is buffered.

By buffered we mean, that the output is collected in a variable and written to the disk at once. Non-buffered entities, continuously write output to the disk without collecting it in a variable.

Clog is used to write messages that are not critical but needs a proper description. However, events or errors that are too critical like system crash need to be written to the output immediately. In this case, we use cerr.

We have demonstrated the use of clog I/O operation in the following coding Example.

 #include <iostream>
#include <fstream>

using namespace std;

int main()
{
               char fileName[] = "data.txt"
               ifstream infile(fileName);

               if(infile)
                              cout << infile.rdbuf();
 
else
                             clog << "Error while opening the file " << fileName << endl; 
return 0;
}

Output:

Error while opening the file data.txt

Here we provide a filename “data.txt” in a variable and try to open this file. If the file is successfully opened, then the contents of the file are read in a buffer. If the file cannot be opened, then a log message is displayed by the clog operation.

You need to note that clog also uses the stream insertion operator as the cout operation.

We have demonstrated the usage of the cerr operation in the below example.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char fileName[] = "input.txt";
ifstream infile(fileName);

if(infile)
cout << infile.rdbuf();
else
cerr << "Cannot open file:" << fileName <<endl;

return 0;
}

Output:

Cannot open file:input.txt

In the above program, we try to open a different file “input.txt”. We read the file if it is opened successfully. If the file opening is not successful then the message is displayed to a standard device that is the display screen saying “cannot open input.txy”.

Conclusion

This is all about basic Input/Output operations in C++. We will discuss a few more important concepts in C++ in our upcoming tutorials.

=> Watch Out The Complete List Of C++ Tutorials In This Series.

Was this helpful?

Thanks for your feedback!

Leave a Comment