iostream头在c ++中的cout,cerr,clog之间有什么区别?什么时候使用哪一个? [英] What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?
问题描述
我试图研究cout,cerr,clog在互联网上的区别,但找不到一个完美的答案。我还不清楚什么时候使用。任何人都可以通过简单的程序解释我,说明什么时候使用哪一个的完美情况?
I tried researching for the difference between cout, cerr, clog on the internet but couldn't find a perfect answer. I still am not clear when to use which. Can anyone explain me through simple programs an illustrate the perfect situation of when to use which one?
我访问过 http://www.tutorialspoint.com/cplusplus/cpp_basic_input_output.htm ,它显示了cerr和clog上的一个小程序,但是在那里获得的输出也可以通过使用cout 。
I visited http://www.tutorialspoint.com/cplusplus/cpp_basic_input_output.htm which shows a small program on cerr and clog, but the output obtained over there can also be obtained by using cout. So, I'm confused of each ones exact use.
推荐答案
stdout和stderr是不同的流,即使他们都指的是控制台输出。重定向(管道)其中一个(例如 program.exe> out.txt
)不会影响其他。
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.
通常,stdout应用于实际程序输出,而所有信息和错误消息应打印到stderr,以便如果用户将输出重定向到文件,信息消息仍然打印在屏幕上而不是输出文件。
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.
这篇关于iostream头在c ++中的cout,cerr,clog之间有什么区别?什么时候使用哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!