Virtual Memory File Reader  16th October 2006

Associated Files: VMemFile.zip

Traditionally, whenever there is a requirement to read characters from a file, the tool of choice is the standard library file input/output stream class (fstream). Good though this stream class is, users invariably have to handle all file operations themselves, typically writing a class to encapsulate the details.

The CVirtualMemFile class associated with this article, presents an alternative file handling approach - it maps a file to virtual memory and allows the contents to be accessed as if the file is a large array of characters. Note, on Windows XP, the maximum file size is limited to the maximum size of partition supported by the OS. This size depends on the file system format - the NTFS maximum partition size is 2Tb and hence the maximum file size supported is 2Tb minus the size of the MFT.

The CVirtualMemFile class relies on two Windows API functions - CreateFileMapping and MapViewOfFile. The former function, returns a handle for the newly created mapping object associated with the file specified in the Open function. The latter function actually maps the view into the address space of the calling process. When called successfully, MapViewOfFile returns the starting address of the mapped view.

To use the class in your code, declare a new CVirtualMemFile object and call the Open function, specifying the full path and file name of the target file. Also, you must specify the access mode you require - read only or read and write access.

CVirtualMemFile VMFile;
VMFile.Open(strFileName, CVirtualMemFile::READ_WRITE_MODE);

If the file is opened successfully, you then need to retrieve the file starting address and file size, as detailed below.

char* pBaseAddr = static_cast<char *>(VMFile.GetBasePointer());
unsigned int VMFileSize = VMFile.GetFileSize();

With the above information, you can then retrieve characters from the file using the starting address as per the example below.

char Character;
Character = pBaseAddr[Position];

The position in the above example is the index of the character you want to retrieve from the file. Obviously, don't specify an index greater than the size of the mapped file (VMFileSize in the above example).

Use of the Virtual Memory File Reader class is particularly appropriate on occasions where you need to iterate through a file character by character. For instance, in a simple XML parser, you could iterate through the target file recording the index of each tag. When data from a particular tag is required, a search could be performed by accessing each tag in turn via the corresponding tag index.

The source code for the Virtual Memory File Reader is available on the downloads page of this site.