cost

In fact, there are three main ones that you usually use: cout, cerr, and clog. First, let ’s briefly introduce the next three.

These three are all output tools provided in the standard IO library in C ++ (the relevant overloading issues are not discussed here):

cout: ostream object written to standard output;

cerr: ostream object output to standard error, commonly used for program error information;

clog: It also outputs the standard error stream (this is the same as cerr). It seems that this is rarely used;

 

When it comes to output, the three are different:

1. The output of cout after buffering is the display by default. This is a buffered output, is standard output, and can be redirected (for the meaning of redirection, please refer to the following example);

2. Cerr is output directly without buffering. It is generally used to output error information quickly. It is standard error. By default, it is connected to the standard output stream, but it is not buffered. There is no need to wait until the buffer or new line breaks are displayed. Under normal circumstances, it is not redirected (redirection seems to be controversial, some people say that it is related to the system, I do n’t quite understand it)

 

 

For why there is cerr and clog

  For example, your program encounters the threat of running out of call stack (infinite, no exit recursion).

  You say, where do you borrow memory to store your error messages?

  So with cerr. Its purpose is to be supported by the output function in emergency situations where you need it most.

  The purpose of the buffer is to reduce the number of screen refreshes-for example, your program outputs an article in the Bible. Without buffering, every time a letter is written, a letter is output and the screen is swiped. With buffering, you will see several sentences "simultaneously" appear on the screen (from memory to video memory, and then refresh the screen).

 

Attached:

#include <iostream>

using namespace std;

int main ()

{

cout << "cout" << endl;

cerr << "cerr" << endl;

return 0;

}

After running this program, we execute the following commands on the command line:

Assume that the executable file after compilation and operation is named cerr.exe, and its directory is E: \ cpro \ cerr \ Debug \ cerr.exe

At the command line, switch to this directory and execute the command: cerr> test.log

The command line output is as follows:

E:\cpro\cerr\Debug>cerr>>test2.log

Cerr (this is output from the command line)

Check the test2.log file and find that there is only

Cost

One line.

Perhaps this is the so-called redirected output.

It can be seen from this:
cout is output on the terminal display. The cout stream correspondingly opens a buffer in the memory to store the data in the stream. When an endl is inserted into the cout stream, regardless of whether the buffer is overflowed, it is immediately Output all the data in the stream, and then insert a newline character. 

The cerr stream object is a standard error stream, which is specified to be associated with the monitor.It has the same effect as cout.It
is a little different from that cout  is usually passed to the monitor output, but can be redirected to the file, and the information in the cerr stream can only be output on the monitor. The 
clog stream is also a standard error stream.The function is the same as cerr.The difference is that cerr does not pass the buffer and outputs information directly to the display.The information in the clog is stored in the buffer.The buffer is full or output when it encounters endl.

I saw these again when I watched C ++ Primer (Fourth Edition) today, excerpt a paragraph for your reference:

"The standard library defines four IO objects. When processing input, an istream type object named cin is used. This object also becomes standard input. When processing output, an ostream type object named cout is used. This object is also called standard output. Standard The library also defines two other ostream objects, named cerr and clog. The cerr object is also called standard error, and is usually used to output warnings and error messages to users of the program, while the clog object is used to generate general information about program execution. Under normal circumstances, the system associates these objects with the execution window, so that when we read from cin, the data is read from the window of the execution program, and when written to cout, cerr, clog, the output is written to the same window. Run Programs, most operating systems provide a way to redirect input or output streams. Redirection can be used to associate these streams with the selected file. "

Transfer from https://blog.csdn.net/qq_38556370/article/details/80640845

Guess you like

Origin www.cnblogs.com/xxxsans/p/12758111.html