Your buffer points to nothing, allocate an `array[4, byte]` and pass its 
address to readBuffer.

I suggest you use streams if you need to save your position/restart your 
processing.

For example this is [how I read the first 6 
bytes](https://github.com/mratsim/Arraymancer/blob/2e7b193ee0554cbbca510a18be16f5564671803c/src/io/io_npy.nim#L80-L85)
 for a magic string for a binary format.
    
    
    proc read_npy*[T: SomeNumber](npyPath: string): Tensor[T] {.noInit.} =
      if unlikely(not existsFile(npyPath)):
        raise newException(IOError, &".npy file \"{npyPath}\" does not exist")
      
      let stream = newFileStream(npyPath, mode = fmRead)
      defer: stream.close()
      
      # Check magic string
      var magic_string: array[6, char]
      discard stream.readData(magic_string.addr, 6)
      
      doAssert magic_string == ['\x93', 'N', 'U', 'M', 'P', 'Y'],
        "This file is not a Numpy file"
    
    
    Run

Reply via email to