writelog.cpp

来自「监听分析流量中的http流量」· C++ 代码 · 共 123 行

CPP
123
字号
// WriteLog.cpp: implementation of the CWriteLog class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WriteLog.h"

#include <windows.h>
#include <winbase.h>
#include <shlwapi.h>
#include <stdlib.h>
#pragma comment(lib, "shlwapi.lib")

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWriteLog::CWriteLog()
{

}

CWriteLog::~CWriteLog()
{

}

/*************************************************************************************************************************/
/*                                              文件处理模块(begin)                                                      */
/*************************************************************************************************************************/



bool CWriteLog::CheckTarPath(char *strFilePath)
{
	//先判断文件夹是否存在
	if(PathFileExistsA((LPCTSTR)strFilePath))
	{
		return TRUE;
	}

	char *pFilePath = strFilePath;
	
	char sPath[512] = {0};
	while(1)
	{
		char *ptemp = strstr(pFilePath, "\\");
		if(!ptemp)
			break;

		int nLen = ptemp - pFilePath;
		char temp[256] = {0};
		strncpy(temp, pFilePath, nLen);
		if(strlen(sPath))
		{
			strcat(sPath, "\\");
			strcat(sPath, temp);
		}
		else
			strcat(sPath, temp);

		ptemp++;
		if(!ptemp)
			break;
		
		if(!PathFileExists((LPCTSTR)sPath))
		{
			if(!CreateFileDirectory(sPath))
				return false;
		}

		pFilePath = ptemp;
	}
	return true;
}


bool CWriteLog::CreateFileDirectory(char *strPath)
{
	if(CreateDirectory((LPCTSTR)strPath,NULL))
		return true;
	return false;
}

//写入日志文件
bool CWriteLog::WriteLogFile(char *strFileName, char *strInfo, int nBuffer)
{


	FILE *fp = NULL;
	try
	{
		if (nBuffer<1)
			return FALSE;
		
		fp = fopen(strFileName, "a+");
		if (!fp)
			return false;
	
		fwrite(strInfo, nBuffer, 1, fp);

		fwrite("\r\n", strlen("\r\n"), 1, fp);

		fclose(fp);
	}
	catch (...)
	{
		printf("出错了");
		DWORD err = GetLastError();
		printf("error = %d", err);
		if(fp)
			fclose(fp);
	}
	
	if(fp)
		fclose(fp);
	return true;
}

/*-----------------------------------------------------------------------------------------------------------------------*/
/*                                              文件处理模块(end)                                                        */
/*-----------------------------------------------------------------------------------------------------------------------*/

⌨️ 快捷键说明

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