我开辟了一个1024k的空间,一直循环写数据,但数据超过1024k后就会崩,,
组长告诉我要判断,当大小超出以后,需要扩大文件大小,用MapViewOfFile()修改文件指针偏移量.
我不知道LPVOID WINAPI MapViewOfFile(
__in HANDLE hFileMappingObject,
__in DWORD dwDesiredAccess,
__in DWORD dwFileOffsetHigh,
__in DWORD dwFileOffsetLow,
__in SIZE_T dwNumberOfBytesToMap
);
dwFileOffsetHigh, dwFileOffsetLow这两参数应该怎么写.
int main()
{
HANDLE hFile = CreateFile(_T("1.txt"), GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (hFile == INVALID_HANDLE_VALUE) {
puts("fail to create file");
goto fail_open;
}
SetFileValidData(hFile, 1024);
HANDLE hMap = CreateFileMapping(hFile, 0, PAGE_READWRITE, 0, 1024, 0);
if (hMap == NULL) {
puts("fail to create mapping");
goto fail_map;
}
LPVOID pContent = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 1024);
if (pContent == 0) {
puts("fail to open mapping");
goto fail_view;
}
// 此处是一个循环,一直在往文件里写数据.
char* pStr = (char*) pContent;
strcpy(pStr, "221221221");
UnmapViewOfFile(pContent);
fail_view:
CloseHandle(hMap);
fail_map:
CloseHandle(hFile);
fail_open:
return 0;
}