What is best way to read and interpret binary files?

welkam wwwelkam at gmail.com
Tue Nov 20 11:54:59 UTC 2018


On Monday, 19 November 2018 at 22:14:25 UTC, Neia Neutuladh wrote:
>
> Nothing stops you from writing:
>
>     SomeStruct myStruct;
>     fd.rawRead((cast(ubyte*)&myStruct)[0..SomeStruct.sizeof]);
>
> Standard caveats about byte order and alignment.

Never would I thought about casting struct to static array. If I 
understood correctly you cast myStruct pointer to ubyte pointer 
and then construct static array on stack with tmpArray.ptr = 
(ubyte pointer) and tmpArray.sizeof = SomeStruct.sizeof

What I figured out when I woke up is that I never needed c style 
arrays. What I could do is to allocate enough data for all file 
in ubyte array and just use slices to read data by chunks and 
cast them into necessary structs.

Thanks Neia Neutuladh and H. S. Teoh for giving me some pointers
https://www.explainxkcd.com/wiki/index.php/138:_Pointers


More information about the Digitalmars-d-learn mailing list