how to append log details in log file in MFC (VC++)?
i have created one method to write to Log file but each time it overrite the log details , i want to create a new entry everytime for logging details.My log method is as below :
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);
开发者_C百科}
}
Any help is highly appreciated . Thanks in Advance.
Open file with "a" (append) instead of "w".
Open file with a+
instead of a
.
精彩评论