It is strongly recommended that you wrap code that uses a file view in exception handling. This is in case the backing file becomes unavailable, such as a network cable unplugged, etc.Code:#include <windows.h> #include <stdio.h> /* * Map a file for read access. Returns size of view in lpcbSize */ LPVOID MapFileRead(LPCTSTR szFileName, size_t * lpcbSize) { HANDLE hFile, hMapping; DWORD dwFileSize; LPVOID lpView; MEMORY_BASIC_INFORMATION mbi; *lpcbSize = 0; hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hFile) { return NULL; } dwFileSize = GetFileSize(hFile, NULL); if (INVALID_FILE_SIZE == dwFileSize) { CloseHandle(hFile); return NULL; } hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (NULL == hMapping) { CloseHandle(hFile); return NULL; } lpView = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); CloseHandle(hMapping); CloseHandle(hFile); if (NULL != lpView) { if (VirtualQuery(lpView, &mbi, sizeof(mbi)) >= sizeof(mbi)) { *lpcbSize = min(dwFileSize, mbi.RegionSize); } else { *lpcbSize = dwFileSize; } } return lpView; } /* * Close a file mapping view. */ BOOL MapFileClose(LPCVOID lpView) { return UnmapViewOfFile(lpView); } int main(void) { size_t cbSize, i; const char * file_view; file_view = (const char *) MapFileRead(TEXT("File Mapping2.cpp"), &cbSize); if (file_view) { try { for (i = 0;i < cbSize; i++) { printf("%c", file_view[i]); } } catch (...) { printf("Oh oh, not good doctor."); } MapFileClose(file_view); } getchar(); return 0; }