std::cerr and std::wcerr














































std::cerr and std::wcerr



C++ std::cerr

The cerr object in C++ is an object of class ostream. It is associated with the standard C error output stream stderr.

cerr declaration

extern std::ostream cerr;

It is defined in <iostream> header file.

The cerr object is ensured to be initialized during or before the first time an object of type ios_base::Init is constructed. After the cerr object is constructed, the expression (cerr.flags & unitbuf) is non zero, which means that any output sent to these stream objects is immediately flushed to the operating system. Also cerr.tie() == &cout i.e. cerr.tie() returns &cout which means that cout.flush() is executed before any output operation on cerr.

The "c" in cerr refers to "character" and 'err' means "error", hence cerr means "character error".

The cerr object is used along with the insertion operator (<<) in order to display a stream of characters. The general syntax is:

cerr<<varName;

or

cerr<<"Some String";

cerr<<var1<<"Some String"<<var2<< endl;

Beginner C++ programmers use cout to display the error using standard output to debug their programs, but it is always good practice to use cerr to display errors.

This is because instead of showing the error stream to the screen, you can later change the error stream to write the errors to a file.

 Example : How cerr works

#include<iostream>
#include<fstream>
using namespace std;
int main(){
    char fileName[] = "data.txt";
    ifstream infile(fileName);
    if(infile){
        cout<<infile.rdbuf();
    }
    else{
        std::cerr<<"Error while opening the file "<<fileName<<endl;    
    }
    return 0;
}

 Output:

When you run the program, the output will be: [if the file could not be opened]

Error while opening the file data.txt


C++ std::wcerr

The wcerr object in C++ is an object of class ostream. It is associated with the standard C error output stream stderr.

Difference between cerr and wcerr

cerr uses char(narrow character) as character type. It can be used for ASCII and ANSI characters.

For internationalization, we need Unicode strings which do not fit in char. wcerr uses wchar_t(wide character) and usable for Unicode characters.

Difference between wcout and wcerr

Beginner C++ programmers use cout and wcout to display the error using standard output to debug their programs, but it is always good practice to use cerr and wcerr to display errors.

This is because instead of showing the error stream to the screen, you can later change the error stream to write the errors to a file.

wcerr declaration

extern wostream wcerr;

It is defined in <iostream> header file.

The wcerr object is ensured to be initialized during or before the first time an object of type ios_base::Init is constructed. After the wcerr object is constructed, the expression (wcerr.flags & unitbuf) is non zero, which means that any output sent to these stream objects is immediately flushed to the operating system. Also wcerr.tie() == &wcout i.e. wcerr.tie() returns &wcout which means that wcout.flush() is executed before any output operation on wcerr.

The "wc" in wcerrrefers to "wide character" and 'err' means "error", hence wcerr means "wide character error". The wcerr object is used along with the insertion operator (<<) in order to display a stream of characters. The general syntax is:

wcerr<<varName;

or

wcerr<<"Some String";

The extraction operator can be used more than once with a combination of variables, strings and manipulators (like endl):

wcerr<<var1<<"Some String"<<var2<<endl;

 

Example : How wcerr works?



#include<iostream>
#include<fstream>
using namespace std;
int main(){
    char fileName[] = "data.txt";
    wifstream infile(fileName);
    if(infile){
        wcout<<infile.rdbuf();
    }
    else{
        std::wcerr<<"Error while opening the file "<<fileName<<endl;
    }
    return 0;
}

 Output:

When you run the program, the output will be: [if the file could not be opened]

Error while opening the file data.txt

More Articles of More Rakesh:

Name Views Likes
std::dec 457 1
std::clog 723 1
std::scanf 539 0
std::fixed 3370 0
std::freopen 532 0
std::fclose 287 1
std::cerr and std::wcerr 765 0
Std:cout 400 1
std::fprintf() 580 0
std::setw and std::setfill 1148 0
std::defaultfloat 602 0
std::endl 245 0
std::cin 294 1
std::fread() 645 0
std::fopen() 615 1

Comments