簡單封裝的一個文件操作的類【原創】


//=========================================================
//
// Copyright (c) 2000-2004  iWise Technologies,Co. Ltd.
// All Rights Reserved.
//
// Product: iW988
// File: myfile.h
// Created: 天衣有縫
// 
// Description:
//     ValueAdded main program for iW988.
//                   Contact:
//                       [email protected]
//
//=========================================================
#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:
//                       [email protected]
//
//=========================================================


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





發佈了41 篇原創文章 · 獲贊 1 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章