简单的程序日志打印函数WriteLog,适用于vc等,含宽字符。

94 篇文章 0 订阅
90 篇文章 1 订阅
#include <STDIO.H>
#include <Windows.h>
#include <Psapi.h>
#include <TCHAR.H>

#pragma  comment(lib,"Psapi.lib")

TCHAR	szLogPath[1000];
TCHAR	szCurrentDirectory[1000];

bool WriteLog(TCHAR *pLog)
{
	FILE *fScream = _tfopen(szLogPath,TEXT("a+"));
	if (fScream!=NULL)
	{
		_ftprintf(fScream,TEXT("%s\n"),pLog);
		fclose(fScream);
		fScream = NULL;
		return true;
	} 
	else
	{
		return false;
	}
}

bool MakeGlobalPath()
{
	DWORD	dwGetModuleFileName = 0,dwGetModuleBaseName = 0;
	TCHAR	szGetModuleFileName[1000];
	DWORD	dwIndex = 0,dwPosition = 0;
	TCHAR	szGetModuleBaseName[500],szBaseName[500];

	dwGetModuleFileName = GetModuleFileName(NULL,szGetModuleFileName,1000);
	
	for (;dwIndex<dwGetModuleFileName;dwIndex++)
	{
		if (szGetModuleFileName[dwIndex] == TEXT('\\'))
		{
			dwPosition = dwIndex;
		}
	}
	_tcsncpy(szCurrentDirectory,szGetModuleFileName,dwPosition+1);
	szCurrentDirectory[dwPosition+1] = TEXT('\0');
	_tprintf(TEXT("szCurrentDirectory:%s\n"),szCurrentDirectory);

	dwGetModuleBaseName = GetModuleBaseName(GetCurrentProcess(),NULL,szGetModuleBaseName,500);
	for (dwIndex = 0;dwIndex<dwGetModuleFileName;dwIndex++)
	{
		if (szGetModuleBaseName[dwIndex] == TEXT('.'))
		{
			dwPosition = dwIndex;
		}
	}
	_tcsncpy(szBaseName,szGetModuleBaseName,dwPosition);
	szBaseName[dwPosition] = TEXT('\0');		//如果没有这句话,就要使用memset给szBaseName初始化。

	_stprintf(szLogPath,TEXT("%s%sLog.txt"),szCurrentDirectory,szBaseName);
	_tprintf(TEXT("szLogPath:%s\n"),szLogPath);
	return true;
}


void main()
{
	MakeGlobalPath();
	WriteLog(TEXT("unicode"));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值