任何人都可以解释cerr cout和clog之间的区别以及为什么会提出不同的对象?
我知道差异如下:
1)cout可以重定向但cerr不能
2)阻塞可以使用缓冲区.
我对第2点感到困惑,如果有人能详细说明,我很感激.
在试图找出如何回答/sf/ask/2352096911/时,我注意到了一个回答的链接一篇相关的SO帖子.我用g ++ 4.8.4尝试了上面链接的答案中的代码,并在程序终止之前得到了分段错误.
这是程序:
#include <iostream>
#include <fstream>
int main()
{
std::ofstream of("cout.txt");
std::cout.rdbuf(of.rdbuf());
std::cout << "test. test. test." << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
构建程序的命令:
g++ -Wall -std=c++11 -g socc.cc -o socc
Run Code Online (Sandbox Code Playgroud)
输出来自gdb:
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the …Run Code Online (Sandbox Code Playgroud) 什么时候使用std::clog,并std::cerr在C++?
clog << "test" << endl;和之间有什么区别cerr << "test" << endl;?
难道clog << "0"没有endl直接打印不带缓冲?
我的代码中有一条基本的调试消息,用于打印有关调用什么函数的消息。
#ifdef _DEBUG
std::clog << "message etc" << std::endl;
#endif
Run Code Online (Sandbox Code Playgroud)
如何重定向输出以将消息发送到文本文件?