//=========================================================
//
// Copyright (c) 2000-2004 iWise Technologies,Co. Ltd.
// All Rights Reserved.
//
// Product: iW988
// File: myfile.h
// Created: 天衣有缝
//
// Description:
// ValueAdded main program for iW988.
// Contact:
//
//=========================================================
#pragma once class CMyFile
{
public:
CMyFile();
virtual ~CMyFile(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 __int64 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:
//
//=========================================================
#include "StdAfx.h"
#include "myfile.h"
#include <assert.h>
CMyFile::CMyFile() : m_hFile(NULL), m_pFileName(NULL)
{
}
CMyFile::~CMyFile(void)
{
Close();
}
BOOL CMyFile::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 CMyFile::Close()
{
if (m_hFile)
{
CloseHandle(m_hFile);
m_hFile = NULL;
}
if (m_pFileName)
{
delete[] m_pFileName;
m_pFileName = NULL;
}
return 0;
}
UINT CMyFile::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 CMyFile::Write(const void* lpBuf, UINT nCount)
{
assert(m_hFile);
DWORD dwReceive = 0; //实际写入的字节数
if (m_hFile)
{
WriteFile(m_hFile, lpBuf, nCount, &dwReceive, NULL);
}
return dwReceive ;
}
__int64 CMyFile::GetLength() const
{
if (!m_hFile)
return 0;
ULARGE_INTEGER liSize =
{
0, 0
};
liSize.LowPart = ::GetFileSize(m_hFile, &liSize.HighPart);
return (__int64) liSize.QuadPart;
}
char* CMyFile::GetFileName()
{
return m_pFileName;
}