Search code examples
c++cstructd

how to read binary file data using dlang


I'm trying to read struct data from specific position in a a binary file. Found out that I can use import std.stdio and its File, however all i seem to find is all about string handling.

I have c-code written data on binary files that compose of several different structs and they all, as far as I understand coding lay in a oneliner. In order to find specific struct I need to, like in old c,

  1. Open file for reading .... (binary read??)
  2. using sizeof and move to startposition of struct data to read
  3. read data (struct.sizeof data) into receivingbuffer and
  4. Close file

Documentation for std.stdio.File # read talks about reading entire or up to size but can't find how to read as below c-code line?

fseek(filehandle, sizeof(firstStructData), SEEK_SET));
read(filehandle, (char *)nextReceivingBuffer, sizeof(nextReceivingBuffer))

Any ideas or hints?


Solution

  • Try File.seek and File.rawRead. They work like their C counterparts, but rawRead determines the read count from the size of the output buffer you hand it.