Differencebetween Cout, Cerr, Clog of iOStream Header in C++? When to Use Which One

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

stdout and stderr are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g. program.exe >out.txt) would not affect the other.

Generally, stdout should be used for actual program output, while all information and error messages should be printed to stderr, so that if the user redirects output to a file, information messages are still printed on the screen and not to the output file.

The question regarding cerr cout and clog

Buffered output is typically much faster than unbuffered. So if you wanted to write a vast amount of data quickly to a log (but didn't care if it actually ended up there), you would use clog rather than cerr.

And all streams can normally be redirected, assuming a vaguely competent operating system, but this is outwith the C++ standard, which has no such concept as "redirection".

How are iostream objects cin, cout, cerr, and clog implemented?

There's no compiler magic.

IIRC, the standard implementation is to define a global constant object in the header. In each translation unit this header is included, one such object is created. Its constructor increments a counter, its destructor decrements it. When incrementing from 0 to 1, the the console stream objects are created, when decrementing from 1 to 0, they are destroyed.

What does the c mean in cout, cin, cerr and clog?

The "c" stands for "character" because iostreams map values to and from byte (char) representations. [Bjarne Stroustrup's C++ Style and Technique FAQ]

Are standard output streams in C++ thread-safe (cout, cerr, clog)?

The article makes a claim about the POSIX standard for the fprintf API. It says nothing about C++ streams. And this is quite correct, as there are no such guarantees on those stream.

Note that although the logging class in that article uses C++ stream syntax, it does this via a std::ostringstream object that is created and destroyed for every logging event, and so is not shared between threads. It uses fprintf to actually write the content to the console.

The Microsoft C library makes some claims to be POSIX compliant, and so the code in the article probably is quite widely portable (as many other popular operating systems are POSIX compliant). But this doesn't mean that standard C++ streams are thread-safe.

Is `clog` buffered?

It's not a bug, clog is not supposed to be buffered, in fact the standard does not associate clog with cout in any way nor does it state tha clog should be buffered.

It does state that cerr and clog are both associated with stderr, so if there was to be some similarity in the behavior it should be between clog and cerr, which seems to be what you are experiencing.

As stderr is defined to be unbuffered it would be logic that clog is also unbuffered, but as this behavior isn't specified, it is not guaranteed to be always the case.

cout is associated with stdout, and its buffering settings are deppendent on the environment. Usually, for UNIX systems, it's line buffered.

29.4.3 Narrow stream objects [narrow.stream.objects]

ostream cout;

3 - The object cout controls output to a stream buffer associated with the object stdout, declared in <cstdio>(29.12.1).

ostream cerr;

4 - The object cerr controls output to a stream buffer associated with the object stderr, declared in <cstdio>(29.12.1).

[...]

ostream clog;

6 - The object clog controls output to a stream buffer associated with the object stderr, declared in <cstdio>(29.12.1).

Outputting to a stdout and a file with cout, cerr, clog, or even user defined ostreams

Your requirement change the normal behavior of cout, which makes the codes confusing.
Why not use another class to do this.Like this :

#include <fstream>
#include <iostream>

class GeneralOStream
{
public:
GeneralOStream(std::ostream& stdStream, std::ostream& fileStream)
:_stdStream(stdStream)
, _fileStream(fileStream)
{
}

template<typename T>
friend GeneralOStream& operator << (GeneralOStream& stream, const T& t);

private:
std::ostream& _stdStream;
std::ostream& _fileStream;
};

template<typename T>
GeneralOStream& operator << (GeneralOStream& stream, const T& t)
{
stream._stdStream << t;
stream._fileStream << t;

return stream;
}

int main()
{
std::ofstream file("out.txt");
GeneralOStream gos(std::cout, file);
std::cout << "Hellow world!" << std::endl; //This outputs to console and file
}


Related Topics



Leave a reply



Submit