ash*_*ash 2 c++ file-io logging visual-c++
如何将日志写入大小为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");
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个更好的选择.
编写一个接受日志字符串的包装类,将它们写入当前日志文件并保留一个总字符串长度的计数器.
达到阈值后,关闭当前日志文件,创建一个新文件,然后重置计数器.
您可以使用编号名称方案,例如log00001.txt, log 00002.txt, ....