大文件操作

工作中碰到需要处理超过2GB的文件,考虑到还要跨平台

封装C库函数成一个类,提供常见的一般IO操作

  1 //Header File
  2 #ifndef __LARGE_FILE_H
  3 #define __LARGE_FILE_H
  4 
  5 //This Marco forces all file access calls to use the 64 bit variants
  6 #define FILE_OFFSET_BITS 64 
  7 
  8 #include <cstdio>
  9 #include <string>
 10 
 11 class CLargeFile {
 12 public:
 13     CLargeFile();
 14     CLargeFile(const std::string &strFile, const char *mode = "rb+");
 15     ~CLargeFile();
 16     bool Open(const std::string &strFile, const char *mode = "rb+");
 17     bool IsOpen(){ return m_bOpen; }
 18     size_t Read(void *ptr, size_t size, size_t nmemb);
 19     size_t Write(const void *ptr, size_t size, size_t nmemb);
 20     char *GetLine(char *s, int size);
 21     int Seek(off_t offset, int whence);
 22     int Flush();
 23     off_t Tell();
 24     void ReWind();
 25     void Close();
 26 private:
 27     FILE *m_hFile;
 28     bool m_bOpen;
 29 };
 30 
 31 #endif
 32 
 33 //Source File
 34 
 35 #include "largefile.h"
 36 
 37 CLargeFile::CLargeFile()
 38     : m_hFile(NULL)
 39     , m_bOpen(false)
 40 {
 41 }
 42 
 43 CLargeFile::CLargeFile(const std::string &strFile, const char *mode)
 44     : m_hFile(NULL)
 45     , m_bOpen(false)
 46 {
 47     Open(strFile, mode);
 48 }
 49 
 50 CLargeFile::~CLargeFile()
 51 {
 52     Close();
 53 }
 54 
 55 bool CLargeFile::Open(const std::string &strFile, const char* mode)
 56 {
 57     Close();
 58 
 59     if(strFile.empty())
 60         m_bOpen = false;
 61     else {
 62         FILE *hRet = fopen(strFile.c_str(), mode);
 63         if(hRet == NULL) {
 64             m_bOpen = false;
 65             m_hFile = NULL;
 66         } else {
 67             m_bOpen = true;
 68             m_hFile = hRet;
 69         }
 70     }
 71 
 72     return m_bOpen;
 73 }
 74 
 75 size_t CLargeFile::Read(void *ptr, size_t size, size_t nmemb)
 76 {
 77     if(!m_hFile)
 78         return 0;
 79 
 80     return fread(ptr, size, nmemb, m_hFile);
 81 }
 82 
 83 size_t CLargeFile::Write(const void *ptr, size_t size, size_t nmemb)
 84 {
 85     if(!m_hFile)
 86         return 0;
 87 
 88     return fwrite(ptr, size, nmemb, m_hFile);
 89 }
 90 
 91 char *CLargeFile::GetLine(char *s, int size)
 92 {
 93     if(!m_hFile)
 94         return NULL;
 95 
 96     return fgets(s, size, m_hFile);
 97 }
 98 
 99 int CLargeFile::Seek(off_t offset, int whence)
100 {
101     if(!m_hFile)
102         return -1;
103 
104     return fseeko(m_hFile, offset, whence);
105 }
106 
107 int CLargeFile::Flush()
108 {
109     if(!m_hFile)
110         return -1;
111 
112     return fflush(m_hFile);
113 }
114 
115 off_t CLargeFile::Tell()
116 {
117     if(!m_hFile)
118         return -1;
119 
120     return ftello(m_hFile);
121 }
122 
123 void CLargeFile::ReWind()
124 {
125     if(!m_hFile)
126         return;
127 
128     rewind(m_hFile);
129 }
130 
131 void CLargeFile::Close()
132 {
133     if(m_hFile) {
134         fclose(m_hFile);
135         m_hFile = NULL;
136         m_bOpen = false;
137     }
138 }
原文地址:https://www.cnblogs.com/jojodru/p/2652511.html