What is best way to read and interpret binary files?
H. S. Teoh
hsteoh at quickfur.ath.cx
Tue Mar 30 04:12:35 UTC 2021
On Tue, Mar 30, 2021 at 12:32:36AM +0000, 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:
> >
> > SomeStruct myStruct;
> > fd.rawRead((&myStruct)[0 .. 1]);
> >
> > This works for all POD types.
> >
> > Writing the struct out to file is the same thing:
> >
> > SomeStruct myStruct;
> > fd.rawWrite((&myStruct)[0 .. 1]);
>
>
> This works, but I'm just wondering why we do not just add more
> functions to the library:
>
> rawRead(ref T t), and
> rawWrite(ref T t)
>
> to read & write single value.
If you wish, submit a PR for this.
It's not hard to write your own overloads for it, though:
void rawWrite(File f, ref T t) @trusted
{
f.rawWrite((cast(ubyte*) &t)[0 .. T.sizeof]);
}
// ditto for rawRead
T
--
A linguistics professor was lecturing to his class one day.
"In English," he said, "A double negative forms a positive. In some
languages, though, such as Russian, a double negative is still a
negative. However, there is no language wherein a double positive can
form a negative."
A voice from the back of the room piped up, "Yeah, yeah."
More information about the Digitalmars-d-learn
mailing list