zoukankan      html  css  js  c++  java
  • c++读文件

    FileFormatDefine.h

    #ifndef _WIN32TOOLS_FILEFORMATDEFINE_H_ 
    #define _WIN32TOOLS_FILEFORMATDEFINE_H_ 
      
    const char FILE_END_CHAR    = 26; 
      
    #endif _WIN32TOOLS_FILEFORMATDEFINE_H_ 

    FileReader.h

    #ifndef _WIN32TOOLS_FILEREADER_H_ 
    #define _WIN32TOOLS_FILEREADER_H_ 
      
    #include <string> 
    #include "FileFormatDefine.h" 
      
    namespace Win32Tools 
    { 
        class CFileReader 
        { 
        public: 
            static char* ReadBuffer(const std::string& aFileName); // 读取文件至内存 
        }; 
    }; 
      
    #endif _WIN32TOOLS_FILEREADER_H_ 
    #include "StdAfx.h" 
    #include "Filereader.h" 
    #include <stdio.h> 
      
    using namespace std; 
      
    namespace Win32Tools 
    { 
        char* CFileReader::ReadBuffer(const string& aFileName) 
        { 
            // 若要一个 byte 不漏地读入整个文件,只能采用二进制方式打开 
            FILE* pFile = fopen (aFileName.c_str(), "rb"); 
            if(pFile == NULL) 
                return NULL; 
      
            // 获取文件大小 
            fseek(pFile , 0 , SEEK_END); 
            long lSize = ftell (pFile); 
            rewind(pFile); 
            if(0 == lSize) 
                return NULL; 
      
            // 分配内存存储整个文件 
            char* buffer = new char[lSize + 1]; 
            if(buffer == NULL) 
                return NULL; 
      
            /* 将文件拷贝到buffer中 */
            size_t result = fread(buffer, 1, lSize, pFile); 
            if(result != lSize) 
            { 
                delete buffer; 
                buffer = NULL; 
                return NULL; 
            } 
            buffer[lSize] = FILE_END_CHAR; 
      
            // 关闭文件并释放内存 
            fclose (pFile); 
            return buffer;   
        } 
    } 
  • 相关阅读:
    python+requests+excel 接口测试
    Pycharm配置git
    ubuntu16.04+ROS安装kinectV1
    ubuntu16.04安装有道词典
    ROS kinetic语音识别
    在Ubuntu16.04中python环境下实现tab键补全
    ros kinetic安装rbx1
    ubuntu14.04安装opencv3.1
    ubuntu16.04SSH无法连接
    VC6中函数点go to definition报告the symbol XXX is undefined
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/3525511.html
Copyright © 2011-2022 走看看