25.文件当做内存来操作

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 #include <boost/interprocess/file_mapping.hpp>
 4 #include <boost/interprocess/mapped_region.hpp>
 5 #include <fstream>
 6 #include <cassert>
 7 #include <cstdlib>
 8 
 9 using namespace std;
10 using namespace boost;
11 
12 //#pragma comment(lib,"libboost_date_time-vc141-mt-sgd-x32-1_66.lib")
13 
14 void main()
15 {
16     //创建读写模式
17     boost::interprocess::mode_t mode = boost::interprocess::read_write;
18     char path[50] = "1.txt";
19     //文件映射
20     boost::interprocess::file_mapping fm(path, mode);
21     //用region访问
22     boost::interprocess::mapped_region region(fm, mode, 0, 0);
23 
24     char *pbegin = reinterpret_cast<char *>(region.get_address());
25     char *pend = pbegin + region.get_size();
26     for (char *p = pbegin; p != pend; p++)
27     {
28         cout << *p << endl;
29     }
30     cin.get();
31 }
原文地址:https://www.cnblogs.com/xiaochi/p/8690904.html