Reading uint[] array from binary file

Heinz billgates at microsoft.com
Mon Jan 22 14:33:11 PST 2007


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.
> 
> in C, I use the following instructions
> 
> #define NBCHANNELSMAX = 4096
> unsigned int spectrum[NBCHANNELSMAX];
> fread(&spectrum, sizeof(unsigned int), nbChannels, file_in);
> 
> but in D, AFAIK, I can only read ubyte[], and the code:
> 
> uint arruint[];
> arruint.length = nbChannels;
> file_in.read(arruint);
> 
> gave me the following error:
> function std.stream.Stream.read (ubyte[]) does not match parameter types 
> (uint[])
> 
> 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..
> 
> Thanks in advance!
> Best regards
> 
> nicolas


Hi, i'm not a great wizard in binary I/O. There's no read() that takes uint[] as parameter. Look at http://www.digitalmars.com/d/phobos/std_stream.html
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.

Hope it work.
G luck.


More information about the Digitalmars-d-learn mailing list