Sourcestd/mmfile.d
MmFile;import std.file;std.file.write(deleteme,"hello");// deleteme is a temporary filenamescope(exit) remove(deleteme);// Use a scope class so the file will be closed at the end of this functionscope mmfile =newMmFile(deleteme);writeln(mmfile.length);// "hello".length// Access file contents with the slice operator// This is typed as `void[]`, so cast to `char[]` or `ubyte[]` to use itconst data =cast(const(char)[]) mmfile[];// At this point, the file content may not have been read yet.// In that case, the following memory access will intentionally// trigger a page fault, causing the kernel to load the file contentswriteln(data[0 .. 5]);// "hello"
import std.file;scope(exit) remove(deleteme);scope mmfile =newMmFile(deleteme,MmFile.Mode.readWriteNew, 5,null);writeln(mmfile.length);// 5auto data =cast(ubyte[]) mmfile[];// This write to memory will be reflected in the file contentsdata[] = '\n';mmfile.flush();writeln(std.file.read(deleteme));// "\n\n\n\n\n"
Mode: int;readreadWriteNewreadWritereadCopyOnWritefilename) scope;filename, Modemode, ulongsize, void*address, size_twindow = 0) scope;stringfilename | name of the file. If null, an anonymous file mapping is created. |
Modemode | access mode defined above. |
ulongsize | the size of the file. If 0, it is taken to be the size of the existing file. |
void*address | the preferred address to map the file to, although the system is not required to honor it. If null, the system selects the most convenient address. |
size_twindow | preferred block size of the amount of data to map at one time with 0 meaning map the entire file. The window size must be a multiple of the memory allocation page size. |
length() const;opDollar = length;mode();opSlice();opSlice(ulongi1, ulongi2);opIndex(ulongi);opIndexAssign(ubytevalue, ulongi);