当前位置:网站首页>The difference between cout/cerr/clog
The difference between cout/cerr/clog
2022-07-04 23:07:00 【Programming segment】
cout/cerr/clog All are C++ Standard output stream provided , What's the difference between these three ?
cout: Standard output stream , Generally, the buffer is not refreshed until the buffer is full or the program ends
cerr: Standard output error , Output a statement will flush the buffer once
clog: Standard output log , Relatively speaking, the frequency of refreshing the buffer is not high
Let's start with a simple example
#include<iostream>
int main()
{
std::cout << "output to cout";
std::cerr << "output to cerr";
std::clog << "output to clog";
}
If we run the program directly , Then these three lines will be output to the screen 
however , If we redirect output , that cout and cerr/clog Will be output to different files (0 Represents standard input ,1 Indicates standard output ,2 Represents standard error output )
that , Given the cerr, Why do we have to clog?
We all know that the program input and output will have buffers , The buffer is in memory , The file is on disk , The program accesses memory much faster than the disk . So in order to improve efficiency , The program will output to the buffer first , It will not be written to the disk file until the buffer is full or when the refresh buffer is displayed . But if the buffer is not full , Now the program crashes , Then the data in the buffer will be lost , relatively speaking cerr Error messages will be important , To ensure that the error information is written to the disk file , The program often flushes the buffer .clog Log information is relatively less important , Therefore, the buffer will not be refreshed often ( Be careful : Frequent buffer flushing will slow down the running speed , Because of frequent access to disk content )
std::flush: Refresh buffer
std::endl: Flush the buffer and wrap
Generally, the program will not refresh the buffer until the end of the program , But sometimes we don't want to refresh the buffer but want to add a newline , Then use escape characters \n
#include<iostream>
int main()
{
std::cout << "output to cout\n";
std::cerr << "output to cerr\n";
std::clog << "output to clog\n";
}

边栏推荐
- 数据库基础知识
- Sword finger offer 67 Convert a string to an integer
- 【机器学习】手写数字识别
- P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
- [machine learning] handwritten digit recognition
- Redis getting started complete tutorial: Geo
- A complete tutorial for getting started with redis: hyperloglog
- 剑指 Offer 65. 不用加减乘除做加法
- Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
- HMS core machine learning service
猜你喜欢

LabVIEW中比较两个VI

A complete tutorial for getting started with redis: redis shell

MariaDB的Galera集群应用场景--数据库多主多活

Redis démarrer le tutoriel complet: Pipeline

Redis入门完整教程:客户端通信协议

Analysis of the self increasing and self decreasing of C language function parameters

Redis:Redis的事务

The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal

Redis入门完整教程:键管理

Advanced area of attack and defense world misc 3-11
随机推荐
Tweenmax emoticon button JS special effect
【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
How to choose a securities company? Is it safe to open an account on your mobile phone
Redis入门完整教程:API的理解和使用
UML图记忆技巧
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
HMS core machine learning service
Redis: redis transactions
Qt加法计算器(简单案例)
PS style JS webpage graffiti board plug-in
Excel 快捷键-随时补充
【爬虫】数据提取之xpath
Servlet服务器端和客户端中文输出乱码问题
Redis getting started complete tutorial: hash description
企业如何跨越数字化鸿沟?尽在云原生2.0
MariaDB的Galera集群应用场景--数据库多主多活
Three stage operations in the attack and defense drill of the blue team
Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
剑指 Offer 65. 不用加减乘除做加法
Redis:Redis消息的发布与订阅(了解)