What is the difference between cout, cerr, clog of

2019-01-16 02:19发布

I tried researching the difference between cout, cerr and clog on the internet but couldn't find a perfect answer. I still am not clear on when to use which. Can anyone explain to me, through simple programs and illustrate a perfect situation on when to use which one?

I visited this site which shows a small program on cerr and clog, but the output obtained over there can also be obtained using cout. So, I'm confused over each one's exact use.

8条回答
仙女界的扛把子
2楼-- · 2019-01-16 02:49

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.

查看更多
倾城 Initia
3楼-- · 2019-01-16 02:54

Both cout and clog are buffered but cerr is un-buffered and all of these are predefined objects which are instances of class ostream. The basic use of these three are cout is used for standard input whereas clog and cerr is used for showing errors. The main point why cerr is un-buffered is may be because suppose you have several outputs in the buffer and an error exception is mentioned in the code then you need to display that error immediately which can be done by cerr effectively.

Please correct me if I am wrong.

查看更多
戒情不戒烟
4楼-- · 2019-01-16 02:54

cout is usually used to display some statements on user screen. ex- : cout<<"Arlene Batada";

output:

Arlene Batada

查看更多
放我归山
5楼-- · 2019-01-16 02:55

cerr does not require a buffer, so it is faster than the other ones and does not use the memory that cout uses, but because cout is buffered, it's more useful in some cases. So:

  • Use cout for the standard output.
  • Use cerr to show errors.
  • Use clog for logging.
查看更多
男人必须洒脱
6楼-- · 2019-01-16 02:56

The difference of these 3 streams is buffering.

  1. With cerr, the output flushs
    • immediately (because cerr does not use buffer).
  2. With clog, the output flushs
    • after you finish your current function.
    • explicitly call the function flush.
  3. With cout, the output flushs
    • after you have call to any output streams (cout, cerr, clog).
    • after you finish your current function.
    • explicitly call the function flush.

Please check the following code, and run DEBUG through 3 lines: f(std::clog), f(std::cerr), f(std::out), then open 3 output files to see what happened. You can swap these 3 lines to see what will happen.

#include <iostream>
#include <fstream>
#include <string>

void f(std::ostream &os)
{
    std::cin.clear(); // clear EOF flags
    std::cin.seekg(0, std::cin.beg); // seek to begin

    std::string line;
    while(std::getline(std::cin, line))   //input from the file in.txt
        os << line << "\n";   //output to the file out.txt
}

void test()
{
    std::ifstream in("in.txt");
    std::ofstream out("out.txt"), err("err.txt"), log("log.txt");
    std::streambuf *cinbuf = std::cin.rdbuf(), *coutbuf = std::cout.rdbuf(), *cerrbuf = std::cerr.rdbuf(),
                    *clogbuf = std::clog.rdbuf();

    std::cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
    std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
    std::cerr.rdbuf(err.rdbuf());
    std::clog.rdbuf(log.rdbuf());


    f(std::clog);
    f(std::cerr);
    f(std::cout);

    std::cin.rdbuf(cinbuf);
    std::cout.rdbuf(coutbuf);
    std::cerr.rdbuf(cerrbuf);
    std::clog.rdbuf(clogbuf);
}

int main()
{
    test();
    std::cout << "123";
}
查看更多
老娘就宠你
7楼-- · 2019-01-16 02:57

Generally you use std::cout for normal output, std::cerr for errors, and std::clog for "logging" (which can mean whatever you want it to mean).

The major difference is that std::cerr is not buffered like the other two.


In relation to the old C stdout and stderr, std::cout corresponds to stdout, while std::cerr and std::clog both corresponds to stderr (except that std::clog is buffered).

查看更多
登录 后发表回答