gpt4 book ai didi

c++ - 如何在C++中创建日志文件

转载 作者:行者123 更新时间:2023-11-30 03:03:52 30 4
gpt4 key购买 nike

我需要创建一个日志文件。我不知道必须在日志文件中放入哪些错误。我有以下代码(但我不知道为什么不写到文件末尾)

日志.cpp

#include "log.h"
#include <ctime>
#include <iostream>
using namespace std;
Log::Log(char* filename) {
//ofstream m_stream(filename);
m_stream.open(filename);

}

在 test.cpp 中,我有 pLOg->Write(c)。我不明白为什么要重写文件,为什么不直接写入文件。

void Log::Write(char* logline)
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
m_stream.seekp (0, ios::end);
while ((m_stream.eof())){}
{
m_stream <<"current time: "<< asctime (timeinfo) <<" "<< logline << endl;
}

}

Log::~Log(){

m_stream.close();
}

日志.h

#include <fstream>

using namespace std;

class Log {
public:
Log(char* filename);
~Log();
void Write(char* logline);
private:
ofstream m_stream;
};

最佳答案

m_stream.open(filename, ios_base::app | ios_base::out);

关于c++ - 如何在C++中创建日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9147575/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com