   Memory mapped files
  Hi!I want to use memory mapped files. I can read from files, but how can i write to an file, which dont exist?Thanks, Christian 
  Maybe I don''t understand your questions, but if it doesn''t exist, why not create it first? ;  Ok...I can use memory mapped files, only if the file exist, because the function CreateFileMapping needs to now how big the file is.But if i want to create an new file i can not creat the mapping.I think i must get memory, write to it and then do something with memory mapping. ;  CreateFileMapping() doesn''t really care how big the file is, unless you set both dwMaximumSizeHigh and dwMaximumSizeLow to 0. In that case the maximum size of the memory mapped area is equal to the original file size. However, you can call CreateFileMapping() with a dwMaximumSizeHigh and dwMaximumSizeLow such that they are bigger than the current file size. In that case the file will be expanded by writes to those areas.Here''s some sample code to demonstrate: #include "windows.h"int main(int, char **) {  HANDLE hFile = CreateFile(L"temp.dat", GENERIC_READ | GENERIC_WRITE,    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,   CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);  if (hFile == INVALID_HANDLE_VALUE) return -1;    HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 2048, L"MyMapping");  if (hMapping == NULL) {    CloseHandle(hFile); return -2;  }    LPVOID lpMapAddress = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);  if (lpMapAddress == NULL) {    CloseHandle(hMapping);    CloseHandle(hFile); return -2;  }    char * p = reinterpret_cast<char *>(lpMapAddress);  for (int i = 0; i < 2048; i++) {    p[i] = ''a'';  }  UnmapViewOfFile(lpMapAddress);  CloseHandle(hMapping);  CloseHandle(hFile);    return 0;}I just compiled an ran this. temp.dat did not exist in the running directory before. What happens is I create the file as a new file. Then I create a file mapping associated to the file with a size actually greater than the previous size (2048 is bigger than 0). Then I go and set all the bytes in the file mapping to equal ''a''. When the program finished, there was a file in the directory with exactly 2048 a''s. ;  Nice example, but what if i dont know the exact size of the output file. Lets say i use memory mapping for a log file -> i dont know the exact output size. ;  If you don''t know the exact size you want to be dealing with before hand, memory mapped I/O is probably a poor choice of I/O techniques. Memory mapped I/O is only really suitable if you want fast random access within a file or to share memory between processes.You could, in theory, create and destroy new memory mappings around the file in order to handle dynamic growth, but doing so seems somewhat counter-productive. ;  Ok you convinced me - i will use the normal windows api, if the file dosent exist. 