Re: What is best way to read and interpret binary files?

2021-03-29 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Mar 30, 2021 at 12:32:36AM +, mw via Digitalmars-d-learn wrote: > On Monday, 19 November 2018 at 22:32:55 UTC, H. S. Teoh wrote: > > Actually, the case is unnecessary, because arrays implicitly convert > > to void[], and pointers are sliceable. So all you need is: > > > >

Re: What is best way to read and interpret binary files?

2021-03-29 Thread mw via Digitalmars-d-learn
On Monday, 19 November 2018 at 22:32:55 UTC, H. S. Teoh wrote: Actually, the case is unnecessary, because arrays implicitly convert to void[], and pointers are sliceable. So all you need is: SomeStruct myStruct; fd.rawRead(()[0 .. 1]); This works for all POD types. Writing

Re: What is best way to read and interpret binary files?

2018-11-20 Thread welkam via Digitalmars-d-learn
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;

Re: What is best way to read and interpret binary files?

2018-11-20 Thread Stanislav Blinov via Digitalmars-d-learn
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*))[0..SomeStruct.sizeof]); Standard caveats about byte order and alignment.

Re: What is best way to read and interpret binary files?

2018-11-20 Thread welkam via Digitalmars-d-learn
On Monday, 19 November 2018 at 22:14:25 UTC, Neia Neutuladh wrote: Nothing stops you from writing: SomeStruct myStruct; fd.rawRead((cast(ubyte*))[0..SomeStruct.sizeof]); Standard caveats about byte order and alignment. Never would I thought about casting struct to static array. If I

Re: What is best way to read and interpret binary files?

2018-11-19 Thread Neia Neutuladh via Digitalmars-d-learn
On Mon, 19 Nov 2018 14:32:55 -0800, H. S. Teoh wrote: >> Standard caveats about byte order and alignment. > > Alignment shouldn't be a problem, since local variables should already > be properly aligned. Right, and the IO layer probably doesn't need to read to aligned memory anyway. Struct

Re: What is best way to read and interpret binary files?

2018-11-19 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Nov 19, 2018 at 10:14:25PM +, Neia Neutuladh via Digitalmars-d-learn wrote: > On Mon, 19 Nov 2018 21:30:36 +, 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 > >

Re: What is best way to read and interpret binary files?

2018-11-19 Thread Neia Neutuladh via Digitalmars-d-learn
On Mon, 19 Nov 2018 21:30:36 +, 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"); >

What is best way to read and interpret binary files?

2018-11-19 Thread welkam via Digitalmars-d-learn
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*)