2012-02-05 137 views
1

我需要創建一個日誌文件。我不知道必須在日誌文件中輸入什麼錯誤。 我有以下代碼(但我不知道爲什麼沒有書面方式到文件末尾)如何在C++中創建日誌文件

log.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->寫( 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(); 
} 

log.h

#include <fstream> 

using namespace std; 

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

相關:HTTP://log4cpp.sourceforge。淨/ – 2012-02-05 07:09:28

+0

用'std :: ios :: out |打開std :: ios :: end',所以你從文件結尾開始。 – Xeo 2012-02-05 07:15:58

+0

我應該在哪裏添加它? .open(file,std :: ios :: out | std :: ios :: end) – user1165435 2012-02-05 07:19:59

回答

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

啊,對,'app' ... +1 – Xeo 2012-02-05 07:21:39

+0

我應該在這裏包含哪些類型的錯誤?廣告如何找到這個錯誤? – user1165435 2012-02-05 07:22:47

+0

你犯的錯誤在技術上是一個邏輯錯誤,沒有錯誤代碼會阻止它。否則,只要確保文件正確地打開,如「if(m_stream)...」或「if(m_stream.open()...」 – Duck 2012-02-05 07:26:07