windows内存映射文件

时间:2022-06-27 08:25:25

http://shushanyegui.duapp.com/?p=731

在描述内存映射文件之前 我们先来写一个系统通过I/O来读写磁盘文件的小程序

#include "stdafx.h"
#include <stdlib.h>
#include <windows.h> char* Read(LPCWSTR file)
{
HANDLE hFile = CreateFile(
(LPCWSTR)file,
GENERIC_READ,
,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
DWORD filesize = GetFileSize(hFile, NULL);
int size = filesize + ;
char *buff = (char *)malloc(sizeof(char)*size);
for (int i = ; i<size; i++)
{
buff[i] = ;
}
DWORD readBytes;
BOOL flag = ReadFile(hFile, buff, size, &readBytes, NULL);
if (flag == FALSE)
{
printf("ReadFile failed/n");
return "false";
}
CloseHandle(hFile);
return buff; } BOOL Write(LPCWSTR file, char *con)
{
HANDLE hFile = CreateFile(
(LPCWSTR)file,
GENERIC_WRITE | GENERIC_READ,
,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile == INVALID_HANDLE_VALUE)
{
return false;
}
//TCHAR buff[MAX_PATH];
DWORD wSize;
char buff[MAX_PATH];
sprintf_s(buff, "%s", con);
BOOL flag = WriteFile(hFile, buff, strlen(buff), &wSize, NULL);
if (!flag){
return false;
}
//缓冲区内容强制刷新至设备
if ( == FlushFileBuffers(hFile))
{
printf("Flush failed/n");
}
CloseHandle(hFile);
return true; }
void TcharToChar(const TCHAR * tchar, char * _char)
{
int iLength;
//获取字节长度
iLength = WideCharToMultiByte(CP_ACP, , tchar, -, NULL, , NULL, NULL);
//将tchar值赋给_char
WideCharToMultiByte(CP_ACP, , tchar, -, _char, iLength, NULL, NULL);
} void CharToTchar(const char * _char, TCHAR * tchar)
{
int iLength;
iLength = MultiByteToWideChar(CP_ACP, , _char, strlen(_char) + , NULL, );
MultiByteToWideChar(CP_ACP, , _char, strlen(_char) + , tchar, iLength);
} int main(int argc, char **argv){
LPCWSTR filename = TEXT("test.txt");
char *con = "Hey guys!!";
if (Write(filename, con))
{
MessageBox(NULL, TEXT("Write success!!!"), TEXT("OK"), MB_OK);
} char *buff = Read(filename);
TCHAR str[]; CharToTchar(buff, str); if (str)
{
TCHAR readBuff[MAX_PATH];
wsprintf(readBuff, TEXT("%s"), str);
OutputDebugString(readBuff);
MessageBox(NULL, readBuff, TEXT("OK"), MB_OK);
}
else {
MessageBox(NULL, TEXT("Oops! There happends something!!"), TEXT("ERROR"), MB_OK);
}
return ;
}

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

http://baike.so.com/doc/6784090-7000692.html

lpMapAddress 对地址进行操作,就相当与操作文件了。
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
typedef struct __ListNode
{
char data[];
struct __ListNode *pNext;
}ListNode, *pListNode; int main(int argc, char **argv)
{
LPCWSTR file = TEXT("test.txt");
HANDLE hFile = CreateFile( //创建或打开文件内核对象
file,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
); if (hFile == INVALID_HANDLE_VALUE)
{
return false;
}
/*
DWORD dwWriteSize;
pListNode curr; //当前节点指针
pListNode head; //首节点
head = NULL;
char temp2[20] = { 0 };
for (int i = 0; i<10; i++)
{
curr = (pListNode)malloc(sizeof(ListNode));
sprintf(temp2, "%.1d\n", i);
strcpy(curr->data, temp2);
curr->pNext = head;
head = curr;
}
pListNode s;
s = head;
wchar_t buff[2] = L"";
while (s)
{
DWORD size = MultiByteToWideChar(CP_ACP, 0, s->data, -1, NULL, 0);
wchar_t *temp1 = (wchar_t *)malloc(size*sizeof(wchar_t));
memset(temp1, 0, size*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, s->data, -1, temp1, size);
WriteFile(hFile, temp1, sizeof(temp1), &dwWriteSize, NULL);
s = s->pNext;
}
/**/
DWORD filesize = GetFileSize(hFile, NULL);
//创建一个文件映射内核对象
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, , filesize, NULL);
if (hMap == INVALID_HANDLE_VALUE)
{
return false;
}
//将文件数据映射到进程的地址空间
LPSTR lpMapAddress = (LPSTR)MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, , , filesize);
if (lpMapAddress == NULL)
{
printf("MapView error!\n");
return -;
}
else
{
int m = ;
printf("----%d\n", filesize);
while (true)
{
if (m > filesize)
{
break;
}
if (lpMapAddress[m])
{
printf("%c\n", lpMapAddress[m]);
lpMapAddress[m] = ;//可以更改文件的值
}
m++; }
} UnmapViewOfFile(lpMapAddress);
CloseHandle(hMap);
CloseHandle(hFile);
return ;
}

下面是linux的版本

#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
int main(){
int File;
struct stat sb;
unsigned char *startAddr; File = open("./OPWIN.bin",O_RDONLY);
//File = open("./test.txt",O_RDONLY);
fstat(File,&sb); printf("--------%d\n",sb.st_size);
startAddr=(unsigned char *)mmap(NULL,sb.st_size,
PROT_READ,MAP_PRIVATE,File,); //printf("%s",startAddr);
munmap(startAddr,sb.st_size);
close(File); }