⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 outinfotofile.cpp

📁 多文档界面设计配置文件读写串口操作串口封装类
💻 CPP
字号:
#include "stdafx.h"
#include "OutInfoToFile.h"

// COutInfoToFile.cpp: implementation of the COutInfoToFile class.
//////////////////////////////////////////////////////////////////
// 版权所有:
// 作者:
// E-mail:
// 声明:自由使用、修改,但必须包括本声明!
// 2007-05-14
// Expect bugs.
// 
// Please use and enjoy. Please let me know of any bugs/mods/improvements 
// that you have found/implemented and I will fix/incorporate them into this
// file. 
//////////////////////////////////////////////////////////////////

//要想使用输出信息到文件,请取消下面这条语句的注释
//如果不想使用输出信息到文件,请注释下面这条语句
#define _EnableOutInfo

//先定义_EnableOutInfo才能使用此全局函数输出信息到文件
void AfxOutInfoToFile(CString csInfo)
{
#ifdef _EnableOutInfo
	COutInfoToFile::AddInfo(csInfo);
#endif
}


CString COutInfoToFile::m_csDriverSign="D:\\";	
CString COutInfoToFile::m_csDirectory="@VC++ TraceInfo\\";//信息文件的主目录
CString COutInfoToFile::m_csProjectName="";//各个项目的子目录
CString COutInfoToFile::m_csFileName="";
bool COutInfoToFile::m_sbAlreadyExist=false;	
void COutInfoToFile::AddInfo(const CString &csInfo)
{
	if(!InitStatus())return;
	
	CStdioFile file;
	CFileException ex;
	if(!file.Open(m_csFileName, CFile::normal | CFile::modeReadWrite |
		CFile::typeText, &ex) )
	{
		AfxMessageBox(csInfo);
		return;
	}
	file.SeekToEnd();
	CString timestr;
	//CTime time;
	//time=CTime::GetCurrentTime();
	//timestr.Format("%4d-%02d-%02d %02d:%02d:%02d",
	//time.GetYear(),time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
	SYSTEMTIME SysTime;
	GetLocalTime(&SysTime);
	timestr.Format("%4d-%02d-%02d %02d:%02d:%02d:%03d",
		SysTime.wYear,SysTime.wMonth,SysTime.wDay,SysTime.wHour,SysTime.wMinute,SysTime.wSecond,SysTime.wMilliseconds);
	timestr = timestr+" "+csInfo;
	file.WriteString(timestr+"\n");
	file.Close();
}

//将所有准备工作做好,如:目录的创建,文件名的创建
bool COutInfoToFile::InitStatus()
{
	if(m_sbAlreadyExist)return true;

	if(!CheckDirectory())return false;
	SpecifyInfoFileName();

	CStdioFile file;
	//if(!file.Open(m_csFileName,CFile::modeCreate))
	if(!file.Open(m_csFileName,CFile::normal | CFile::modeReadWrite |
		CFile::typeText))
	{
		if(!file.Open(m_csFileName,CFile::modeCreate))
		{
		   AfxMessageBox("错误信息文件:"+m_csFileName+"没有成功创建!");
		   return false;
		}
	}

	file.Close();

	m_sbAlreadyExist=true;
	return true;
}

//判断目录是否存在,不存在就试着创建一个
//Success: true
//Fail:	false
bool COutInfoToFile::ExistDirectory(const CString& csDirFullPath)
{
	CFileFind finder;
	
	if(!finder.FindFile(csDirFullPath+"*") )
	{	//不存在就创建
		if(!CreateDirectory(csDirFullPath,NULL))
		{
			AfxMessageBox("错误信息文件的目录:"+csDirFullPath+"不能被创建!");
			return false;
		}
	}

	return true;//目录已存在、创建
}

//检测目录有没有
//没有就创建出来
bool COutInfoToFile::CheckDirectory()
{
	CString csDirectoryFullPath=m_csDriverSign+m_csDirectory;

	if( !ExistDirectory(csDirectoryFullPath) )
	{	//没有这个目录
		return false;
	}
	
	GetApplicateSelfName();
	csDirectoryFullPath += m_csProjectName;
	if( !ExistDirectory(csDirectoryFullPath) )
	{	//没有这个目录
		return false;
	}

	return true;
}

//将文件名指定出来
void COutInfoToFile::SpecifyInfoFileName()
{
/*	m_csFileName = "";
	m_csFileName += m_csDriverSign;	//盘符
	m_csFileName += m_csDirectory;	//规定的总目录
	m_csFileName += m_csProjectName;//项目名做子目录
	
	CTime time;
	time=CTime::GetCurrentTime();
	m_csFileName += time.Format("Info %b%d-%Y");
	m_csFileName += ".txt";*/

	CTime time;
	time=CTime::GetCurrentTime();
	m_csFileName += time.Format("Info %b%d-%Y");
	m_csFileName += ".txt";
	char FullPath[255]; 
	int len = GetModuleFileName(NULL,FullPath,255); 
    strcpy(FullPath+len-13,m_csFileName);
	m_csFileName = FullPath;
}

//指定应用程序自身的名字作为子目录
void COutInfoToFile::GetApplicateSelfName()
{
	m_csProjectName = AfxGetAppName();
	m_csProjectName += "\\";
}

⌨️ 快捷键说明

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