使用内存映射文件来提高你程序的性能

参考一:http://baike.baidu.com/view/394293.htm

参考二:http://www.cnblogs.com/fangyukuan/archive/2010/09/09/1822216.html

练习代码:
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
 
int main(int argc, CHAR* argv[])
{
   // Open the file that we want to map.
   // 注意请在c盘,自己创建一个kuan.txt文件,并写入内容
   HANDLE hFile = CreateFile("C:\\test.txt",
      GENERIC_READ | GENERIC_WRITE,
      0,
      NULL,
      OPEN_ALWAYS,
      FILE_ATTRIBUTE_NORMAL,
      NULL);
 
   // Create a file-mapping object for the file.
   HANDLE hFileMapping = CreateFileMapping(hFile,
      NULL,
      PAGE_WRITECOPY,
      0, 0,
      NULL);

   PBYTE pbFile = (PBYTE)MapViewOfFile(hFileMapping, FILE_MAP_COPY, 0, 0, 0);
   if(pbFile != NULL){
   cout << pbFile << endl;
   }
   UnmapViewOfFile(pbFile);
 
   CloseHandle(hFileMapping);
   CloseHandle(hFile);
 
   return 0;
}


你可能感兴趣的:(c,object,File,null,2010)