What is best way to read and interpret binary files?

mw mingwu at gmail.com
Tue Mar 30 00:32:36 UTC 2021


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.


> For arrays:
>
> 	SomeStruct[] arr;
> 	fd.rawWrite(arr);
> 	...
>
> 	arr.length = ... /* expected length */
> 	fd.rawRead(arr);

Currently, the library only have this two functions for arrays.




More information about the Digitalmars-d-learn mailing list