簡體   English   中英

如何在C ++ / VC ++中將日志寫入64KB大小的文件?

[英]How to write logs to files of size 64KB in C++/VC++?

如何將日志寫入大小為64KB的文件(允許記事本讀取)。一旦文件達到64KB,它應該開頭創建另一個,另一個......文件名可以自動生成。

我嘗試了以下代碼

static int iCounter=1;
CString temp;
      static CStdioFile f(L"c:\\Log1.txt", CFile::modeWrite | CFile::modeRead |  CFile::modeCreate | CFile::modeNoTruncate);

 int nlength = (int)f.GetLength();
 if(nlength>(nMaxFileSize*1024))
 {
     //need to create a new file
     f.Close();
     iCounter++;
     temp.Format(_T("%s%d%s"), "c:\\Log",iCounter, ".txt");
     f.Open(temp,CFile::modeWrite | CFile::modeRead | CFile::modeCreate | CFile::modeNoTruncate);

 }
 f.SeekToEnd();
 f.WriteString(str);
 f.WriteString(L"\r\n");

我正在尋找一個更好的選擇。

編寫一個接受日志字符串的包裝類,將它們寫入當前日志文件並保留一個總字符串長度的計數器。

達到閾值后,關閉當前日志文件,創建一個新文件,然后重置計數器。

您可以使用編號名稱方案,例如log00001.txt, log 00002.txt, ...

使用log4cplus當然可以處理它 - 正確配置。

http://log4cplus.sourceforge.net/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM