如何在MFC(VC ++)的日志文件中附加日志详细信息?

时间:2010-11-15 09:08:58

标签: visual-c++ mfc

我创建了一个写入日志文件的方法,但每次覆盖日志详细信息时,我都想每次创建一个新条目来记录详细信息。我的日志方法如下:

void CNDSConnectDlg::WriteLogData()
{


    CString strUserName = "";
    m_editUserName.GetWindowText(strUserName);
    FILE * pFile = NULL;        
    int iErr = 0;
    iErr = fopen_s(&pFile,"NDSLog.txt","w");

    if (iErr == 0)
    {

    CString strConnectionStatus = "";
    CString strServerAddress = "";
    CString strDateTime = "";
    SYSTEMTIME systime;
    GetLocalTime(&systime); 


    if(m_bConnectionStaus == true)
    {
       strConnectionStatus = "Success";
    }
    else
    {
       strConnectionStatus = "Failure";
    }

    strUserName.Format("%s",strUserName);
    strConnectionStatus.Format("%s",strConnectionStatus); 
    strServerAddress.Format("%s",m_strIPAddress);
    strDateTime.Format("%i:%i:%i\\%02i-%02i-%02i",
            systime.wHour,
            systime.wMinute,
            systime.wSecond,
            systime.wYear,
            systime.wMonth,
            systime.wDay);

    fputs("UserName = " + strUserName + " connected to "
          "ServerAddress = " +strServerAddress + " at "
          "Date/Time = " + strDateTime + " "  
          "ConnectionStatus = " +strConnectionStatus + " ",pFile); 


    fclose (pFile); 
    }
    else
    {
         MessageBox("Error in writing to Log","NDS",MB_ICONERROR | MB_OK);

    }

}

非常感谢任何帮助。 在此先感谢。

2 个答案:

答案 0 :(得分:0)

使用“a”(追加)而不是“w”打开文件。

答案 1 :(得分:0)

使用a+而不是a打开文件。