MapFileView示例

来源:互联网 发布:回文c语言 编辑:程序博客网 时间:2024/06/05 12:04

代码功能介绍:
1.创建一新文件:test.txt,并向文件写入256kb的内容

2.利用内存映射文件映射从135kb开始之后的1kb内容,由于文件映射的起始地址
必须是系统分配粒度的整数倍,所以必须根据系统分配粒度对预设的起始地址
做必要的调整.

3.在文件的135kb处写入内容:i love hello kitty

#include <windows.h>#include <stdio.h>#include <iostream>using namespace std;#define BUFFSIZE 1024 #define FILE_MAP_START 138240  // 135kTCHAR szFileName[] = TEXT("test.txt");  // file nameint main(void){    //-----------------------------------------------------    // 创建文件    //-----------------------------------------------------    HANDLE hFile = CreateFile(        szFileName,                      // file name        GENERIC_READ | GENERIC_WRITE,    // read/write access        0,                               // not shared        NULL,                            // default security        CREATE_ALWAYS,                   // creates a new file        FILE_ATTRIBUTE_NORMAL,           // normal file attribute        NULL);                           // no template file    if (hFile == INVALID_HANDLE_VALUE)    {        cout << "CreateFile failed with error:"             << GetLastError() << endl;        return 1;    }    //-----------------------------------------------------    // 获取系统分配粒度    //-----------------------------------------------------    DWORD  dwSysGran;    SYSTEM_INFO SysInfo;    GetSystemInfo(&SysInfo);    dwSysGran = SysInfo.dwAllocationGranularity;    //-----------------------------------------------------    // 计算文件映射相关变量    //-----------------------------------------------------    DWORD  dwFileMapSize;  // size of the file mapping    DWORD  dwMapViewSize;  // the size of the view    DWORD  dwFileMapStart; // where to start the file map view    // 计算文件映射的起始地址,必须是系统分配粒度的整数倍    dwFileMapStart = (FILE_MAP_START / dwSysGran) * dwSysGran;    cout << "The file map view starts at "          << dwFileMapStart << " bytes into the file.("         << dwFileMapStart / 1024 << " kb)" << endl;    // 计算文件映射视图的大小    // Note:文件中被映射到进程地址空间中的部分称为视图    dwMapViewSize = (FILE_MAP_START % dwSysGran) + BUFFSIZE;    cout << "The file map view is "         << dwMapViewSize << " bytes large.("         << dwMapViewSize / 1024 << " kb)" << endl;    // 计算文件映射对象的大小    dwFileMapSize = FILE_MAP_START + BUFFSIZE;    cout << "The file mapping object is "         << dwFileMapSize << " bytes large.("         << dwFileMapSize / 1024 << " kb)" << endl;    //-----------------------------------------------------    // 写文件    //-----------------------------------------------------    //写入64*4=256kb的内容    DWORD  dBytesWritten;    for (int i=0; i<64*1024; i++)     {        WriteFile(hFile, &i, sizeof(i), &dBytesWritten, NULL);    }    DWORD  dwFileSize;    dwFileSize = GetFileSize(hFile,  NULL);    cout << "After write file.The file size is "          << dwFileSize << " bytes large.("         << dwFileSize / 1024 << " kb)" << endl;    //-----------------------------------------------------    // 创建file mapping object    //-----------------------------------------------------    HANDLE hMapFile;    hMapFile = CreateFileMapping(         hFile,          // file handle        NULL,           // default security        PAGE_READWRITE, // read/write access        0,              // size of mapping object, high        dwFileMapSize,  // size of mapping object, low        NULL);          // name of mapping object    if (hMapFile == NULL)     {        cout << "CreateFileMapping failed with error:"             << GetLastError() << endl;        return 2;    }    //-----------------------------------------------------    // Map view of file    //-----------------------------------------------------    LPVOID lpMapAddress;    lpMapAddress = MapViewOfFile(        hMapFile,            // handle to file mapping object        FILE_MAP_READ |         FILE_MAP_WRITE,      // read/write access        0,                   // high order DWORD of the file offset        dwFileMapStart,      // low  order DWORD of the file offset        dwMapViewSize);      // number of bytes to map    if (lpMapAddress == NULL)     {        cout << "MapViewOfFile failed with error:"             << GetLastError() << endl;        return 3;    }    //-----------------------------------------------------    // 操作文件    //-----------------------------------------------------    int iViewDelta = FILE_MAP_START - dwFileMapStart;    char *pData = (char*)lpMapAddress + iViewDelta;    sprintf(pData, "i love Hello Kitty");    //-----------------------------------------------------    // Close the file mapping object and the open file    //-----------------------------------------------------    UnmapViewOfFile(lpMapAddress);    CloseHandle(hMapFile);    CloseHandle(hFile);    //-----------------------------------------------------    // exit    //-----------------------------------------------------    system("pause");    return 0;}
0 0