cout ,cerr and clog

closed account (28poGNh0)
what is the difference between cout ,cerr and clog?
They are three streams used for different purposes.

Back in Unix and good old C, there are three "fake" files: Standard Input (keyboard), Standard Output (screen) and Standard Error (also screen).

The main difference between output and error is that the first is buffered (this may produce text artifacts that can be avoided by flushing). The reason why they exist separately is to give programmers and users a way to differentiate error messages from regular messages.

Standard Input in C++ is std::cin (console input).
Standard Output is std::cout (console output).
Standard Error is std::cerr (console error).
std::clog is basically a rarely used clone of std::cerr, intended to be used for logging.
closed account (28poGNh0)
Thanks for reply good explanation

1st : what is flushing?
2nd: what are the cases when for example cout cannot works but cerr works ?

that's what I am looking for because when I write
1
2
3
cout << "Console output" << endl;
    cerr << "Console error" << endl;
    clog << "Console error type 2" << endl;


I get the same result ,I get distingush between the three of them is there some x when It is necessiraly need to choose one of them

hope I express well
1st : what is flushing?


It's when you force the output operation to finish. This is useful for files and streams which are buffered, such as stdout in C and std::cout in C++.

You will usually not need to do this, but in some rare cases you may.

C code:
http://www.cplusplus.com/reference/cstdio/stdout/
http://www.cplusplus.com/reference/cstdio/fflush/
1
2
3
4
5
6
#include <stdio.h>

// ...

fprintf(stdout, "Hello, World!\n"); // same as printf("Hello, World!\n");
fflush(stdout); // flushing 


C++ code:
http://www.cplusplus.com/reference/ostream/endl/
http://www.cplusplus.com/reference/ostream/flush-free/
http://www.cplusplus.com/reference/ostream/ostream/flush/
1
2
3
4
5
6
7
8
#include <iostream>

// ...

std::cout << "Hello, World!" << std::endl; // same as
std::cout << "Hello, World!" << '\n' << std::flush;
// can also do
std::cout.flush();


I get the same result ,I get distingush between the three of them is there some x when It is necessiraly need to choose one of them

You need to use redirection when the program runs to see why they're useful.
http://en.wikipedia.org/wiki/Redirection_%28computing%29

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    std::cout << "Standard Output!\n";
    std::cerr << "Standard Error!\n";
    std::clog << "Standard Log??\n";
}


Run in Command Prompt with:
prog.exe 1> stdout.txt

stdout.txt contains:
Standard Output!


Run with:
prog.exe 2> stderr.txt

stderr.txt contains:
Standard Error!
Standard Log??

closed account (28poGNh0)
Thanks a lot @Catfish4 for the effort I'll try to miss with this code,hope I did not bother
Topic archived. No new replies allowed.