www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading uint[] array from binary file

reply Nico_C <nicolas.charvin libertysurf.fr> writes:
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
Jan 22 2007
parent reply Heinz <billgates microsoft.com> writes:
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.
Jan 22 2007
parent =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
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]
Jan 22 2007