当前位置:网站首页>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";
}
边栏推荐
- Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
- MySQL数据库备份与恢复--mysqldump命令
- The small program vant tab component solves the problem of too much text and incomplete display
- Redis入门完整教程:API的理解和使用
- [graph theory] topological sorting
- [odx Studio Edit pdx] - 0.2 - Comment comparer deux fichiers pdx / odx
- Create Ca and issue certificate through go language
- Redis入门完整教程:客户端通信协议
- How to choose a securities company? Is it safe to open an account on your mobile phone
- 剑指 Offer 65. 不用加减乘除做加法
猜你喜欢
qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
Redis introduction complete tutorial: detailed explanation of ordered collection
Editplus-- usage -- shortcut key / configuration / background color / font size
Complete tutorial for getting started with redis: bitmaps
Redis入门完整教程:Pipeline
EditPlus--用法--快捷键/配置/背景色/字体大小
Redis getting started complete tutorial: Geo
Redis入門完整教程:Pipeline
Redis getting started complete tutorial: publish and subscribe
随机推荐
数据库基础知识
Create Ca and issue certificate through go language
金融市场,资产管理与投资基金
Gnawing down the big bone - sorting (II)
PS style JS webpage graffiti board plug-in
How to choose a securities company? Is it safe to open an account on your mobile phone
String类中的常用方法
Serial port data frame
Three stage operations in the attack and defense drill of the blue team
MariaDB的Galera集群-双主双活安装设置
剑指 Offer 67. 把字符串转换成整数
Redis入门完整教程:事务与Lua
ScriptableObject
qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
Basic knowledge of database
【爬虫】数据提取之xpath
C语言快速解决反转链表
A complete tutorial for getting started with redis: redis shell
剑指Offer 68 - II. 二叉树的最近公共祖先
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等