|
文件影射的方式
//创建文件
HANDLE hFile = CreateFileForMapping(_T("MYTEST.txt"),GENERIC_READ ? GENERIC_WRITE,0,NULL,
OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
ASSERT(hFile != 0);
DWORD dwLenFile = GetFileSize(hFile,NULL);
//创建内存文件对象
HANDLE hMapFile = CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,dwLenFile,L"MyMappingFile");
ASSERT(hMapFile != 0);
//获取内存映射文件对象视图
LPSTR pData = (LPSTR)MapViewOfFile(hMapFile,FILE_MAP_WRITE,0,0,dwLenFile);
ASSERT(pData != NULL);
//*****操作*****
char *pReadBuf = new char[dwLenFile+1];
strcpy(pReadBuf,pData);
pReadBuf[0] = "5 ";
pReadBuf[1] = "2 ";
pReadBuf[2] = "0 ";
strcpy(pData,pReadBuf);
//*****关闭*****
//取消视图
UnmapViewOfFile(pData);
//关闭映射文件
CloseHandle(hMapFile);
//关闭文件
CloseHandle(hFile);
delete[] pReadBuf; |
|