📄 mylogfile.h
字号:
#ifndef MYFILE_H
#define MYFILE_H
#include "StdAfx.h"
#include <assert.h>
//=========================================================
//
// Copyright (c) 2000-2004 iWise Technologies,Co. Ltd.
// All Rights Reserved.
//
// Product: iW988
// File: myfile.h
// Created: 天衣有缝
//
// Description:
// ValueAdded main program for iW988.
// Contact:
// waterpub@mail.csdn.net
//
//=========================================================
//#pragma once class CMyLogFile
class CMyLogFile
{
public:
CMyLogFile();
virtual ~CMyLogFile(void);
BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags) ; // 打开文件
UINT Read(void* lpBuf, UINT nCount) ; // 读文件
DWORD Write(const void* lpBuf, UINT nCount) ; // 写文件
HANDLE GetSafeHandle()
{
if(!m_hFile)
return NULL;
return m_hFile;
}
virtual int GetLength() const; // 文件大小
int Close(); // 关闭文件
char* GetFileName(); // 文件名
private:
HANDLE m_hFile; // 文件句柄
char* m_pFileName; // 全文件名
public:
enum openMode
{
modeRead = 1,
modeWrite = 2
};
};
//=========================================================
//
// Copyright (c) 2000-2004 iWise Technologies,Co. Ltd.
// All Rights Reserved.
//
// Product: iW988
// File: myfile.cpp
// Created: 天衣有缝
//
// Description:
// ValueAdded main program for iW988.
// Contact:
// waterpub@mail.csdn.net
//
//=========================================================
CMyLogFile::CMyLogFile() : m_hFile(NULL), m_pFileName(NULL)
{
}
CMyLogFile::~CMyLogFile(void)
{
Close();
}
BOOL CMyLogFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags)
{
Close();
DWORD dwDesiredAccess;
DWORD dwCreationDisposition;
switch (nOpenFlags)
{
case modeRead:
dwDesiredAccess = GENERIC_READ;
dwCreationDisposition = OPEN_EXISTING;
break;
case modeWrite:
dwDesiredAccess = GENERIC_WRITE;
dwCreationDisposition = OPEN_ALWAYS;
break;
default:
assert(false);
}
HANDLE hFile = CreateFile(lpszFileName, dwDesiredAccess, 0,// 不共享
NULL, dwCreationDisposition,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
m_hFile = hFile;
m_pFileName = new char[strlen(lpszFileName) + 1];
strcpy(m_pFileName, lpszFileName);
return TRUE;
}
else
{
hFile = NULL;
return FALSE;
}
}
int CMyLogFile::Close()
{
if (m_hFile)
{
CloseHandle(m_hFile);
m_hFile = NULL;
}
if (m_pFileName)
{
delete[] m_pFileName;
m_pFileName = NULL;
}
return 0;
}
UINT CMyLogFile::Read(void* lpBuf, UINT nCount)
{
assert(m_hFile);
if (m_hFile)
{
DWORD nNumberOfBytesRead;
if (ReadFile(m_hFile, lpBuf, nCount, &nNumberOfBytesRead, NULL))
{
return (UINT) nNumberOfBytesRead;
}
}
return 0;
}
DWORD CMyLogFile::Write(const void* lpBuf, UINT nCount)
{
assert(m_hFile);
DWORD dwReceive = 0; //实际写入的字节数
if (m_hFile)
{
WriteFile(m_hFile, lpBuf, nCount, &dwReceive, NULL);
}
return dwReceive ;
}
int CMyLogFile::GetLength() const
{
if (!m_hFile)
return 0;
ULARGE_INTEGER liSize =
{
0, 0
};
liSize.LowPart = ::GetFileSize(m_hFile, &liSize.HighPart);
return liSize.QuadPart;
}
char* CMyLogFile::GetFileName()
{
return m_pFileName;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -