#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"));
}
简单的程序日志打印函数WriteLog,适用于vc等,含宽字符。
最新推荐文章于 2024-03-13 10:15:34 发布