Read X many bytes from File to address Y

Adam D. Ruppe destructionator at gmail.com
Wed Apr 7 13:04:14 UTC 2021


On Wednesday, 7 April 2021 at 12:57:12 UTC, tcak wrote:
> Well, I have a struct, that is defined as a variable already. I 
> want to read X bytes from the file (not Struct.sizeof bytes 
> though), and read into the struct variable without any extra 
> buffer.

file.rawRead((cast(ubyte*) &your_struct)[0 .. your_length]);


Of course you can also just use the C function too:

fread(&your_struct, your_length, 1, file.getFP());


the std.stdio is just a wrapper around FILE* so it offers get FP 
to use for the other things.

It just isn't that different really aside from the little cast.


More information about the Digitalmars-d-learn mailing list