What is best way to read and interpret binary files?

welkam wwwelkam at gmail.com
Tue Nov 20 12:47:04 UTC 2018


On Tuesday, 20 November 2018 at 12:01:49 UTC, Stanislav Blinov 
wrote:
> On Tuesday, 20 November 2018 at 11:54:59 UTC, welkam wrote:
>> 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
>
> Almost correct, except it's not a static array, it's just a 
> slice, i.e. ubyte[].

I guess it came from inseparability with C where you want to 
slice C arrays? Thats useful to know


More information about the Digitalmars-d-learn mailing list