简单封装的一个文件操作的类【原创】

王朝vc·作者佚名  2006-01-09
宽屏版  字体: |||超大  

//=========================================================

//

// 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 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:

// waterpub@mail.csdn.net

//

//=========================================================

#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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有