logrecord.cpp
来自「 本实例实现在VC下如何编写串口(com port)实例(与串口通信), 对初」· C++ 代码 · 共 90 行
CPP
90 行
// 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 + =
减小字号Ctrl + -
显示快捷键?