C++带缓存的读文件操作类

145次阅读
没有评论

共计 2217 个字符,预计需要花费 6 分钟才能阅读完成。

File.h

//文件读写操作类,带有缓冲区,从缓冲区读取
#include <string.h>
#include <fstream>
#include <iostream>
using namespace std;
#define BUFFER_SIZE 1024
class File
{

private:
    ifstream  m_file;

public:
    FileReader(const std::string &strPath);
    ~FileReader();

    int Read(char *pBuf, int nLen);

    int SetPos(unsigned int nPos);
    int SkipPos(unsigned int nPos);

    bool IsOpen();

    void CloseFile();
    char * m_pBuffer;

   unsigned int m_currentPos;
   unsigned int m_nFileSize;
   unsigned int m_nPerPos;
   unsigned int m_nPostPos;

};

File.cpp

#include "File.h"


File::File(const std::string &strPath):m_file(strPath)
{
    if(!m_file.is_open())
    {
        std::cout<<"open file failed"<<std::endl;
    }
    m_pBuffer = new char[BUFFER_SIZE];

    m_currentPos = 0;
    m_nPerPos = 0 ;
    m_nPostPos = 0;
    //获取文件大小
    m_file.seekg(0,std::ios::end);
    m_nFileSize = m_file.tellg();
    m_file.seekg(0,std::ios::beg);

}


File::~File()
{
    if(m_pBuffer != NULL)
    {
        delete[] m_pBuffer;
        m_pBuffer = NULL;
    }
}
bool File::IsOpen()
{
    return m_file.is_open();
}
void File::CloseFile()
{
    m_file.close();
}
int File::Read(char *pBuf, int nLen)
{
    // if(m_currentPos >= m_fileSize)
    // {
    //     return -1;
    // }
    // //有缓存
    // unsigned int tellg = m_file.tellg();
    // if(m_currentPos + nLen <= tellg){
    //     memcpy(pBuf, m_buffer + nLen, nLen);
    //     m_currentPos += nLen;
    //     return nLen;
    // }else{
    // //无缓存
    //     if(m_fileSize - m_currentPos >= BUFFER_SIZE){
    //         if(nLen > BUFFER_SIZE){
    //             m_file.read(pBuf, nLen);
    //             m_currentPos += nLen;
    //             return nLen;
    //         }
    //         m_file.read(m_buffer, BUFFER_SIZE);
    //         memcpy(pBuf, m_buffer, nLen);
    //         m_currentPos += nLen;
    //         return nLen;
    //     }else{
    //         m_file.read(m_buffer, m_fileSize - m_currentPos);
    //         memcpy(pBuf, m_buffer, m_fileSize - m_currentPos);
    //         m_currentPos += m_fileSize - m_currentPos;
    //         return m_fileSize - m_currentPos;
    //     }
    // }

    if((m_currentPos >= m_nPerPos) &&
       (m_currentPos <= m_nPostPos) &&
       (m_currentPos + nLen <= m_nPostPos)   
    )
    {
        memcpy(pBuf,m_pBuffer + m_currentPos - m_nPerPos,nLen);
        m_currentPos+=nLen;
            return nLen;
    }else
    {
        if(m_currentPos <= (BUFFER_SIZE /2 ))
        {
            m_nPerPos = 0;
        }else
        {
            m_nPerPos = m_currentPos - (BUFFER_SIZE /2);
        }
        if(m_currentPos + (BUFFER_SIZE / 2 ) >=m_nFileSize )
        {
            m_nPostPos = m_nFileSize;
        }else
        {
            m_nPostPos = m_currentPos + (BUFFER_SIZE / 2);
        }

        if(m_nPostPos - m_currentPos >= nLen)
        {
            m_file.seekg(m_nPerPos,std::ios::beg);
            m_file.read(m_pBuffer,m_nPostPos - m_nPerPos);
            memcpy(pBuf,m_pBuffer + m_currentPos - m_nPerPos,nLen);
            m_currentPos+=nLen;
            return nLen;
        }else
        {
            m_file.seekg(m_currentPos,std::ios::beg);
            m_file.read(pBuf,nLen);
            m_currentPos+=nLen;
            return nLen;
        }
    }


}

 

正文完
 
admin
版权声明:本站原创文章,由 admin 2022-05-14发表,共计2217字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码