C++ Creating a View Within a File

127 篇文章 0 订阅
108 篇文章 1 订阅
#include<windows.h>
#include<stdio.h>
#include<tchar.h>
#include <strsafe.h>
#include<locale.h>

#define BUFFSIZE 1024
#define FILE_MAP_START 138240 //135KB

LPCWCHAR file = TEXT("test.txt");


DWORD failureMessage() {

	DWORD dw = GetLastError();

	LPTSTR lpMsgBuf;
	LPTSTR lpDisplayBuf;


	FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER |
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		dw,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR)&lpMsgBuf,//自动在末尾加 换行符
		0, NULL);

	// Display the error message and exit the process
	lpDisplayBuf = (LPTSTR)LocalAlloc(LMEM_ZEROINIT,
		(lstrlen(lpMsgBuf) + 40) * sizeof(TCHAR));

	StringCchPrintf(lpDisplayBuf,
		LocalSize(lpDisplayBuf) / sizeof(TCHAR),
		L"failed with error %d : %s",
		 dw, lpMsgBuf);


	wprintf(lpDisplayBuf);
		
	LocalFree(lpMsgBuf);
	LocalFree(lpDisplayBuf);

	return dw;
}


int main() {
	setlocale(LC_ALL, "chs");

	HANDLE hMapFile;
	HANDLE hFile;
	BOOL bFlag;
	DWORD dByteWritten;
	DWORD dwFileSize;
	DWORD dwFileMapSize;
	DWORD dwMapViewSize;
	DWORD dwFileMapStart;
	DWORD dwSysGran;

	SYSTEM_INFO SysInfo;

	LPVOID mapAddress;

	char* pData;
	int i;
	int iData;
	int iViewDelta;

	hFile = CreateFile(file, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);	
	//failureMessage();

	// Get the system allocation granularity.
	GetSystemInfo(&SysInfo);
	//failureMessage();
	dwSysGran = SysInfo.dwAllocationGranularity;
	wprintf(L"dwAllocationGranularity:%d\n", dwSysGran);

	dwFileMapStart = (FILE_MAP_START / dwSysGran) * dwSysGran;
	_tprintf(TEXT("The file map view starts at %ld bytes into the file.\n"),
		dwFileMapStart);


	dwMapViewSize = (FILE_MAP_START % dwSysGran) + BUFFSIZE;
	_tprintf(TEXT("The file map view is %ld bytes large.\n"),
		dwMapViewSize);


	dwFileMapSize = FILE_MAP_START + BUFFSIZE;
	_tprintf(TEXT("The file mapping object is %ld bytes large.\n"),
		dwFileMapSize);


	iViewDelta = FILE_MAP_START - dwFileMapStart;
	_tprintf(TEXT("The data is %d bytes into the view.\n"),
		iViewDelta);

	DWORD dBytesWritten;

	for (i = 0; i < (int)dwSysGran; i++)
	{
		WriteFile(hFile, &i, sizeof(i), &dBytesWritten, NULL);
	}


	// Verify that the correct file size was written.
	dwFileSize = GetFileSize(hFile, NULL);
	_tprintf(TEXT("hFile size: %10d\n"), dwFileSize);


	hMapFile = CreateFileMapping(hFile,          // current file handle
		NULL,           // default security
		PAGE_READWRITE, // read/write permission
		0,              // size of mapping object, high
		dwFileMapSize,  // size of mapping object, low
		NULL);          // name of mapping object

	if (hMapFile == NULL)
	{
		_tprintf(TEXT("hMapFile is NULL: last error: %d\n"), GetLastError());
		return (2);
	}
	_tprintf(TEXT("CreateFileMapping OK\n"));

	LPVOID lpMapAddress = MapViewOfFile(hMapFile,            // handle to
													// mapping object
		FILE_MAP_ALL_ACCESS, // read/write
		0,                   // high-order 32
							 // bits of file
							 // offset
		dwFileMapStart,      // low-order 32
							 // bits of file
							 // offset
		dwMapViewSize);      // number of bytes
							 // to map
	if (lpMapAddress == NULL)
	{
		_tprintf(TEXT("lpMapAddress is NULL: last error: %d\n"), GetLastError());
		return 3;
	}
	_tprintf(TEXT("MapViewOfFile OK\n"));

	pData = (char*)lpMapAddress + iViewDelta;

	iData = *(int*)pData;

	_tprintf(TEXT("The value at the pointer is %d,\nwhich %s one quarter of the desired file offset.\n"),
		iData,
		iData * 4 == FILE_MAP_START ? TEXT("is") : TEXT("is not"));


	bFlag = UnmapViewOfFile(lpMapAddress);
	_tprintf(TEXT("UnmapViewOfFile OK\n"));
	bFlag = CloseHandle(hMapFile); // close the file mapping object

	if (!bFlag)
	{
		_tprintf(TEXT("\nError %ld occurred closing the mapping object!"),
			GetLastError());
	}

	bFlag = CloseHandle(hFile);   // close the file itself

	if (!bFlag)
	{
		_tprintf(TEXT("\nError %ld occurred closing the file!"),
			GetLastError());
	}
	_tprintf(TEXT("CloseHandle OK\n"));


	return 0;
}

dwAllocationGranularity:65536
The file map view starts at 131072 bytes into the file.
The file map view is 8192 bytes large.
The file mapping object is 139264 bytes large.
The data is 7168 bytes into the view.
hFile size:     262144
CreateFileMapping OK
MapViewOfFile OK
The value at the pointer is 34560,
which is one quarter of the desired file offset.
UnmapViewOfFile OK
CloseHandle OK

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值