高性能C++服务器

c++ 内存映射大文件

2021-10-10  本文已影响0人  c之气三段

读文件
.h

#ifndef PLOT3DDATAREADER_H
#define PLOT3DDATAREADER_H


#include "FileReader.h"
using namespace std;

class Plot3DDataReader 
{
public:
    Plot3DDataReader();
    ~Plot3DDataReader();
    void readPlot3DDataFile(string file_name);
private:
    void fileMapping(string file_name);
    void getDataList(const char* data,int fileSize);
    void deelWithData();
private:
    list<string> m_flieList;
};

#endif // PLOT3DDATAREADER_H

.cpp

#include"Plot3DDataReader.h"
#include <iostream>
#include <assert.h>

#include <Windows.h>
#include <WinBase.h>
#include<list>
#define BAD_POS 0xFFFFFFFF // returned by SetFilePointer and GetFileSize
#define SUCCESS 0

Plot3DDataReader::Plot3DDataReader()
{

}

Plot3DDataReader::~Plot3DDataReader()
{

}

void Plot3DDataReader::readPlot3DDataFile(string file_name)
{
     fileMapping(file_name);
     deelWithData();
}

void Plot3DDataReader::fileMapping(string file_name)
{
    size_t indexFirst,indexSecond;
    indexFirst = file_name.find_last_of("\\")+1;
    indexSecond = file_name.find_last_of(".");
    size_t count = indexSecond - indexFirst;
    string shared_name = file_name.substr(indexFirst,count);
    const char* fileName = file_name.c_str();
    const char* sharedName = shared_name.c_str();
    cout << "create memorymapfile..." << endl;
       //存取模式
       DWORD access_mode = (GENERIC_READ | GENERIC_WRITE);
       //共享模式
       DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
       //文件属性
       DWORD flags = FILE_FLAG_SEQUENTIAL_SCAN;//|FILE_FLAG_WRITE_THROUGH|FILE_FLAG_NO_BUFFERING;
       DWORD error_code;

       //创建文件
       HANDLE mmHandle = CreateFileA(fileName, access_mode, share_mode, NULL, OPEN_ALWAYS, flags, NULL);

       if (mmHandle == INVALID_HANDLE_VALUE)
       {
           error_code = GetLastError();
           cout << "创建mmf失败:" << error_code << endl;
       }
       else
       {
           DWORD high_size;
           DWORD file_size = GetFileSize(mmHandle, &high_size);
           if (file_size == BAD_POS && (error_code = GetLastError()) != SUCCESS)
           {
               CloseHandle(mmHandle);
               cout << "error:" << error_code << endl;
           }
           cout << "create mmf sucessfully" << endl;

           //创建文件映射,如果要创建内存页面文件的映射,第一个参数设置为INVALID_HANDLE_VALUE
           HANDLE mmfm = CreateFileMappingA(mmHandle, NULL, PAGE_READWRITE, 0, 0, sharedName);

           error_code = GetLastError();
           if (SUCCESS != error_code)
           {
               cout << "createFileMapping error" << error_code << endl;
           }
           else
           {
               if (mmfm == NULL)
               {
                   if (mmHandle != INVALID_HANDLE_VALUE)
                   {
                       CloseHandle(mmHandle);
                   }
               }
               else
               {
                   DWORD view_access = FILE_MAP_ALL_ACCESS;
                   //获得映射视图
                   char* mmfm_base_address = (char*)MapViewOfFile(mmfm, view_access, 0, 0, file_size);
                   if (mmfm_base_address == NULL)
                   {
                       error_code = GetLastError();
                       if (error_code != SUCCESS)
                       {
                           cout << "error code " << error_code << endl;
                       }
                   }
                   else
                   {
                       //数据填充到list中
                       getDataList(mmfm_base_address, file_size);
                       //卸载映射
                       UnmapViewOfFile(mmfm_base_address);
                       //关闭内存映射文件
                       CloseHandle(mmfm);
                       //关闭文件
                       CloseHandle(mmHandle);
                   }
               }
           }
       }
}

void Plot3DDataReader::getDataList(const char* data, int fileSize)
{
    string strStream;
    for (size_t i = 0; i < fileSize; i++)
    {
        strStream.push_back(data[i]);
        if (data[i] == '\n')
        {
            m_flieList.push_back(strStream);
            strStream = "";
        }
    }
}

void Plot3DDataReader::deelWithData()
{
    for(auto data: m_flieList)
    {
        cout<< data <<endl;
    }
}

linux下

https://www.cnblogs.com/cyyljw/p/7009652.html

上一篇下一篇

猜你喜欢

热点阅读