Reading uint[] array from binary file

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Mon Jan 22 14:48:49 PST 2007


Heinz kirjoitti:
> Nico_C Wrote:
> 
>> Hi
>> I am trying to port a tool from C to D, and I have trouble to read data 
>> from binary file, using arrays.

>> So my question is:
>> 1) is it possible to mimic the C fonction fread in order to get an array 
>> of uint, ushort, long, whatever the type ?
>> 2) if not, how can i convert a ubyte[] to uint[], ushort[], etc..
>>
> There's a readExact() function but it only make sence if the array is static (fixed size) and this is not the case, try creating the array with a fixed length.

readExact(void* buffer, uint size) takes a pointer to the buffer. No
need to worry, it also allows dynamic arrays. Example:

  auto f = new File("test.bin", FileMode.In);

  ubyte tmp[];
  tmp.length = 12;
  f.readExact(tmp, 12); // question 1)

  writefln(cast(uint[])tmp); // question 2)

Possible output:

  [1,2,3]


More information about the Digitalmars-d-learn mailing list