What is best way to read and interpret binary files?
Neia Neutuladh
neia at ikeran.org
Mon Nov 19 22:14:25 UTC 2018
On Mon, 19 Nov 2018 21:30:36 +0000, welkam wrote:
> So my question is in subject/title. I want to parse binary file into D
> structs and cant really find any good way of doing it. What I try to do
> now is something like this
>
> byte[4] fake_integer;
> auto fd = File("binary.data", "r");
> fd.rawRead(fake_integer);
> int real_integer = *(cast(int*) fake_integer.ptr);
>
> What I ideally want is to have some kind of c style array and just cast
> it into struct or take existing struct and populate fields one by one
> with data from file. Is there a D way of doing it or should I call
> core.stdc.stdio functions instead?
Nothing stops you from writing:
SomeStruct myStruct;
fd.rawRead((cast(ubyte*)&myStruct)[0..SomeStruct.sizeof]);
Standard caveats about byte order and alignment.
More information about the Digitalmars-d-learn
mailing list