【发布时间】:2015-05-09 19:01:11
【问题描述】:
我在 .bin 文件中有一个非常小的 FAT16 分区。 我已使用 CreateFile、CreateFileMapping 和 MapViewOfFile 将其映射到内存中。
我想要做的是读取文件的特定字节。
例如,我想读取从 0x36 到 0x3A 的偏移量,以检查这是否是 FAT16 分区:
这是我的代码,直到:
#include <Windows.h>
#include <stdio.h>
void CheckError (BOOL condition, LPCSTR message, UINT retcode);
int main(int argc, char *argv[])
{
HANDLE hFile;
HANDLE hMap;
char *pView;
DWORD TamArchivoLow, TamArchivoHigh;
//> open file
hFile =CreateFile (L"disk10mb.bin", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
CheckError(hFile == INVALID_HANDLE_VALUE,"ERROR opening the file", 1);
//> get file size.
TamArchivoLow = GetFileSize (hFile, &TamArchivoHigh);
//> Create the map
hMap = CreateFileMapping (hFile, NULL, PAGE_READWRITE, TamArchivoHigh, TamArchivoLow, NULL);
CheckError(NULL== hMap, "ERROR executing CreateFileMapping", 1);
//> Create the view
pView= (char *) MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, TamArchivoLow);
CheckError(NULL==pView, "ERROR executing MapViewOfFile", 1);
// Access the file through pView
//////////////////////////////////////
//////////////////////////////////////
//>Free view and map
UnmapViewOfFile(pView);
CloseHandle(hMap);
CloseHandle(hFile);
return 0;
}
void CheckError (BOOL condition, LPCSTR message, UINT retcode)
{
if (condition)
{
printf ("%s\n", message);
ExitProcess (retcode);
}
}
【问题讨论】:
-
这些函数仅仅读取 4 个字节就有点过头了。你试过简单的
open() lseek() read() close()吗? -
我要读取更多字节,这只是开始
标签: c windows memory-mapped-files memory-mapping