C++

Cerr C++

Source codes of any programming language may be a source of an error that leads to the interruption in the program’s execution. These errors must be identified and displayed to the user so that the user can rectify the error and make the solution to avoid them. This topic is related to the error displayed as a resultant output in the C++ programming language. C++ standard streams are different for input and output features. For example, the input stream is cin. Whereas there are two output streams, one is the standard output stream (cout), and the second one is the standard error stream (cerr).

Standard output stream: Cout is a part of the ostream class. It is used to display the output on the output device, a display screen in actuality. The data displayed on the output screen is inserted in the output stream using the insertion operator, the double angular bracket.

Standard error stream: The ā€œCā€ in cerr refers to the ‘character’ and err denotes the ‘error.’ It means the character error. That error stream is used to display the error in the source code. This is used when we are in the condition of displaying the error message immediately. It is always recommended to use cerr to display the errors found in the source code.

Cerr syntax

The syntax of cerr is defined below:

Cerr << "error variable/string/message';

<< are the insertion operators used to display the relevant value. At the same time, the variable/string/message is mostly a variable or an array or the containers like lists, etc.

Insertion operator and cerr

The insertion operator assists the cerr in displaying the output. This operator can also be used more than a single time along with the variables or strings as well.

Cerr << var << "message" << var1 << endl;

This is how we get the number of errors identified and displayed through cerr. Because by doing this, we can rectify the error at the initial stage by avoiding them to proceed forward.

Implementation of cerr
Example 1

This example is a sample of the error message displayed in the main program. This will indicate what the error message works and looks like. In this program, firstly, we will use the library as a header file that will allow the program to use the cin, cout, cerr streams so that we will use this input/output stream in the program.

#include <iostream>

In the main program, now a message will be simply displayed. Besides using the cout statement to print the error message, a ‘cerr’ stream will be used here.

Cerr << "ERROR!!"

To see the executed value, first, compile the file by using the compiler we use to compile the C++ language code in the Linux operating system.

$ g++ -o cerr cerr.c
$ ./cerr

The error message is displayed in the terminal.

Example 2

The second example involves a file in the Ubuntu system to get opened through a source code in C++. While using the file management systems in any programming language, we may encounter issues regarding opening the file, closing, or making any update like reading or writing in data. Similarly, this example also relates to such a situation. Like the previous example, the library for the input and output streams is used here. But in addition to this header file, we will also include a file library, as this library is required to perform all the functionalities of files.

#include <iostream>
#include <fstream>

In the main program, we will take a variable of string data type to store the file name of a file with a ‘txt’ extension created and stored in the documents of the Ubuntu operating system. Whenever we need to mention the file’s name, the variable will help out.

Ifstream my_file(file_name);

‘ifstream’ is an input file stream. It is used to read data present in the file. So by using this ifstream along with the file name, you will read all the data present inside the file. Now we will use an if-else statement here to apply a check of having an error or not. So if a part will check if the file of the file name provided by the user is present in the system, then open that file and display its content by removing any sort of error. This is done through the function my_file.rdbuf().

This is the stream buffer linked or associated with the stream, and its function is to clear the flags of the error state.

Whereas in the else part, that will be executed if the ‘if’ part is false, which means the file is not found.

So firstly, we will go for the ‘if’ part, which means a file with the ‘fruit.txt’ name will be accessed through this code. Execute the code in the terminal through the compiler. On execution, you will get the under-mentioned results.

By using an ifstream option, the whole data will be displayed that is present inside the file. Now moving forward to the next step, if such a case occurs when the file name is mistakenly written wrong in the code or any file removed from the system but still mentioned to be opened. It means that the file of that name is not present, or there will be no such file on the computer. For instance, here, we have changed the file’s name ‘fruits.txt’ to ‘fruitt.txt’. So what will happen to the working of ifstream is that it will not be able to access and read that file. The control will now move towards the else part of the statement, and an error message will be displayed.

The file contents are displayed using ‘cout,’ whereas the error message is displayed using ‘cerr’ in the code. Execute the code to see the results.

You will see that an error message is displayed here that shows that the file cannot be opened.

Example 3

This example is quite simple and involves the application or a specific tool to see the results. The code contains two statements. One is a cout. And the other one is cerr. Both are the output statements. While using a tool, the resultant value is displayed in the output console or a window. At the same time, the errors are displayed in a specific error window.

But as we are using the simple text editor to write the code and to execute it in the terminal console, we will not be able to discriminate this result effectively. But the purpose of using this code is to display the use of cerr in this way in the main program. The tool can be visual studio or any other that can be able to compile the C++ codes.

Conclusion

The article ‘cerr C++’ is an article that demonstrates the meaning of cerr and to which stream of C++ programming language it belongs. The working of cerr is explained with the insertion operator we use in our programs to display the errors. Some common and easily understandable examples are highlighted to demonstrate the usage of cerr.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.