问题描述
Hello all Programmer,
任何人都可以帮我用c ++编写日志文件或者为我提供指针或代码片段。
谢谢
Sampath
Hello all Programmer,
Can anybody help me to write log file in c++ or provide me the pointer or some code snippet for same.
thanks
Sampath
推荐答案
#include <fstream>
void write_text_to_log_file( const std::string &text )
{
std::ofstream log_file(
"log_file.txt", std::ios_base::out | std::ios_base::app );
log_file << text << std::end;
}
如果文件由于某种原因无法打开,奖金也是如此在一堆未定义的行为中崩溃。
干杯,
Ash
PS当你自己写的时候记录的重要部分是:
- 每条消息后刷新 - std: :endl这样做
- 在每条消息后关闭文件 - fstream析构函数执行该操作
It'll do the same thing with the bonus that if the file fails to open for whatever reason it won't crash in a steaming heap of undefined behaviour.
Cheers,
Ash
PS the important bits of logging for when you write your own are:
- flush after every message - std::endl does that
- close the file after every message - the fstream destructor does that
这篇关于如何用C ++编写日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!