writeerrorlog.cpp

来自「一个简单的可以实现每天写一个不同文件的日志文件的类。」· C++ 代码 · 共 63 行

CPP
63
字号
// WriteErrorLog.cpp
//
#include "stdafx.h"
#include "WriteErrorLog.h"

/////////////////////////////////////////////////////////////////////////////
// CWriteErrorLog


/////////////////////////////////////////////////////////////////////////////
// CHeelApp construction

CWriteErrorLog::CWriteErrorLog()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

BOOL CWriteErrorLog::WriteLogFile(CString m_strErrorInfo)
{
	FILE   *stream;
	CString strTime;
	CString strFileName;
	CString strTemp;
	CString strWriteInfo;

	CTime timeCurrent  = CTime::GetCurrentTime();   // 获取系统日期
	int   iYear    = timeCurrent.GetYear();         // 获取年份
	int   iMonth   = timeCurrent.GetMonth();        // 获取当前月份
	int   iDay     = timeCurrent.GetDay();          // 获得几号
	int   iHour    = timeCurrent.GetHour();         // 获取当前为几时
	int   iMinute  = timeCurrent.GetMinute();       // 获取分钟
	int   iSecond  = timeCurrent.GetSecond();       // 获取秒

	strFileName.Format("%04d-%02d-%02d.txt", iYear, iMonth, iDay);
	
	strTime.Format("%04d-%02d-%02d %02d:%02d:%02d", iYear, iMonth, iDay, iHour, iMinute, iSecond);

	strWriteInfo = strTime + "   " + m_strErrorInfo;

	/* Open file in text mode: */
	if( (stream = fopen( strFileName, "a" )) != NULL )
	{
		//把文件的位置指针移到文件尾
		fseek(stream, 0L, SEEK_END);
		
		/* Write 25 characters to stream */
		fwrite( strWriteInfo, strWriteInfo.GetLength(), 1, stream );

		strTemp = "\n";
		fwrite( strTemp, strTemp.GetLength(), 1, stream );

		//获取文件长度
		//length=ftell(stream);

		fclose( stream );
		return true;
   }

	return false;
 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?