I found this [example](https://github.com/nim-lang/Nim/issues/4473) but it is 
old and does not work. Any idea? 
    
    
    import memfiles, os
    
    var
      mm, mm_full, mm_half: MemFile
      p: pointer
    
    let fn = "/tmp/test.mmap"
    
    if fileExists(fn): removeFile(fn)
    
    mm = memfiles.open(fn, mode = fmWrite, newFileSize = 20)    # Create a new 
file
    mm.close()
    
    mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1)
    echo "Full read size: ", mm_full.size
    
    p = mm_full.mapMem(fmReadWrite, 20, 0)
    
    var p2 = cast[cstring](p)
    p2[0] = 'H'
    p2[1] = 'e'
    p2[2] = 'l'
    p2[3] = 'l'
    p2[4] = 'o'
    p2[5] = '\0'
    
    mm_full.unmapMem(p, 20)
    mm_full.close()
    
    # read half, and verify data change
    mm_half = memfiles.open(fn, mode = fmRead, mappedSize = 10)
    echo "Half read size: ",mm_half.size, " Data: ", cast[cstring](mm_half.mem)
    mm_half.close()
    
    
    Run

Output: 
    
    
    $ nim c -r test.nim
    ...
    Full read size: 20
    /home/hdias/development/test.nim(17) test
    /usr/local/programs/x86_64/nim-1.4.4/lib/pure/memfiles.nim(82) mapMem
    /usr/local/programs/x86_64/nim-1.4.4/lib/pure/includes/oserr.nim(94) 
raiseOSError
    Error: unhandled exception: Bad file descriptor [OSError]
    Error: execution of an external program failed: 
'/home/hdias/development/test '
    
    
    Run

Reply via email to