Thread overview | |||||
---|---|---|---|---|---|
|
January 22, 2007 Reading uint[] array from binary file | ||||
---|---|---|---|---|
| ||||
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 |
January 22, 2007 Re: Reading uint[] array from binary file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nico_C | 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. |
January 22, 2007 Re: Reading uint[] array from binary file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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] |
Copyright © 1999-2021 by the D Language Foundation