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


//=========================================================
//
// 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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值