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

📄 logrecord.cpp

📁   本实例实现在VC下如何编写串口(com port)实例(与串口通信), 对初学者弄懂串口的实现过程是很好的一个实例.以及现实将串口读到的数据记录到一个testlog目录中,以做备份
💻 CPP
字号:
// Logrecord.cpp : implementation file
//

#include "stdafx.h"
#include "comport.h"
#include "Logrecord.h"
#define BUFSIZE 1204



LogRecord::LogRecord()
{
	//初始化文件创建的变量
	m_nloglength = 0;
	m_strlog = m_strfilename+"\n";
	m_strfilename = "";	
	m_strmode = "w+";  //a+

	CString currdir;
	DWORD dir;
	DWORD buflen;
	TCHAR tchBuffer1[BUFSIZE];   //             TCHAR = char
	LPTSTR lpbuf;
	lpbuf = tchBuffer1;
	dir =  GetCurrentDirectory(buflen,lpbuf);
	currdir.Format("%s",lpbuf);

	//获得当前的时间用于创建目录
	DWORD year,month,day;	
	CTime currtime = CTime::GetCurrentTime();
	year = currtime.GetYear();
	month = currtime.GetMonth();
	day = currtime.GetDay();

	//创建目录
	CString currpath;
	currpath.Format("%s\\testlog\\%d-%d-%d\\",currdir,year,month,day);
	CreateDirectory(currpath,NULL);
	
	//创建文件
	m_strfilename =currpath+"1.log";
	m_strlog = m_strfilename+"\n";
	m_nloglength = strlen(m_strlog);

 	FILE *pFile = fopen(m_strfilename,m_strmode);	
	fwrite(m_strlog, 1, m_nloglength, pFile);
	fclose(pFile);

}


BOOL LogRecord::CreatLog()
{
	
	FILE *pFile = fopen("\\.testlog\\.1.log","a+");

	
//	loglength = strlen("Hello! This my log file.\nYouranme:   Mac Su\nYour card ID:\tF130260");
	//str.Format("loglengtg: %d",loglength);
	//AfxMessageBox(str);
	fwrite("Hello! This my log fi", 1,\
		strlen("Hello! This my log fi"), pFile);
	fclose(pFile);
   
	return TRUE;
}
BOOL LogRecord::CreatLog(char buf[])
{

	return TRUE;
}

void LogRecord::ShowLength()
{
	m_strlog = "Hello My log file\nThis is my log file-----\tsknaf\n;";
	m_nloglength = strlen(m_strlog);
	m_strlog.Format("m_nloglength = %d",m_nloglength);
	AfxMessageBox(m_strlog);
}

BOOL LogRecord::Record(CString str)
{
	m_strlog = str;
	m_nloglength = strlen(m_strlog);
	FILE *pFile = fopen(m_strfilename,"a+");
	fwrite(m_strlog, 1, m_nloglength, pFile);
	fclose(pFile);
	return TRUE;
}

⌨️ 快捷键说明

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