0

I have created a Log file by name "NDSLog" now i wanted that if NDSLog file exceeds its limit a new file should create by name NDSLog1 and so on...Currently i am able to create NDSLog file and when it exceeds it limit, I close that exisiting file & open a new file .

I want to know i can increment NDSLog to NDSLog1 and so on when it reaches that Limit ?

Any help is highly appreciated.

I am using WritetoLog method for this.

long CNDSLog::WriteLogData(char *logData, long lDataSize)
    {

if (m_File == NULL)
{
    GetFileName();
    OpenNewFile();
}
else
{
    long lFileSize = GetFileSize(m_sFileName);
    if (lFileSize > m_lFileSize)
    {
        CloseNewFile(); 
        GetFileName();
        OpenNewFile();
    }
}

WriteData(logData);

return ERR_NONE;

}

long CNDSLog::GetFileName()
{
    char ctemp[300];
int lLen = sprintf(ctemp,"%s",m_sFName.data());

if (lLen > 0)
{       
    if (m_sFileName != NULL)
    {
        delete [] m_sFileName;
        m_sFileName = NULL;
    }

    m_sFileName = new char[lLen + 1];
    memset(m_sFileName,0,lLen + 1);
    memcpy(m_sFileName,ctemp,lLen);
}

return ERR_NONE;

};

long CNDSLog::OpenNewFile()
{
    if (m_sFileName != NULL)
    {
        char strPathName[_MAX_PATH];
        ::GetModuleFileName(NULL, strPathName, _MAX_PATH);      

    // The following code will allow you to get the path.
    CString newPath(strPathName);
    int iPos = newPath.ReverseFind('\\');

    if (iPos != -1)
    newPath = newPath.Left(iPos+1);     
    newPath += "NDSLog\\" ;


    if (GetFileAttributes(newPath) == INVALID_FILE_ATTRIBUTES)
    {
      CreateDirectory(newPath,NULL);
    }   
    newPath +=  m_sFileName;        

    m_File = fopen(newPath,"at");
}
return ERR_NONE;

}

1 Answer 1

0

An esay way is to have a counter in your WriteLogData () function int nCounter passing it to GetFileName(). When you are vreating the filename you could add the counter like that:

sprintf(ctemp,"%s_%d",m_sFName.data(), nCounter);

Now you have your 1, 2, ...

Now your WriteLogData () should be like that:

long CNDSLog::WriteLogData(char *logData, long lDataSize)
    {

in nCounter = 1; // or whatever
if (m_File == NULL)
{
    GetFileName(nCounter);
    OpenNewFile();
}
else
{
    long lFileSize = GetFileSize(m_sFileName);
    if (lFileSize > m_lFileSize)
    {
        nCounter++;
        CloseNewFile(); 
        GetFileName(nCounter);
        OpenNewFile();
    }
}

WriteData(logData);

return ERR_NONE;
3
  • But how it feel create a ew file with name NDSLog1 and so on...it just increment the counter ... Dec 8, 2010 at 6:31
  • No because you updated the signatyure for GrtFileName() function as well, it takes the counter as a parameter and use it to create the actual file name.
    – Sunscreen
    Dec 8, 2010 at 8:38
  • No, it is not taking that updated filename and creating a new one , for creating a new file something like Createfile or some other function has to be used.how to that part ? That part is missing here i guess. Dec 8, 2010 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.