www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Read X many bytes from File to address Y

reply tcak <tcak gmail.com> writes:
I am talking about std.file.File.

I have opened the file, and at a specific offset.

I want to read X many bytes from the file, and want it to be 
written to given address directly without any Magical D-stuff 
like ranges etc.

Is there a way to do this without getting into C or Posix header 
files?

There is rawRead, but it takes an array as parameter, which 
causes a dirty looking code with cast etc!
Apr 07 2021
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 7 April 2021 at 11:42:56 UTC, tcak wrote:
 There is rawRead, but it takes an array as parameter, which 
 causes a dirty looking code with cast etc!
What did you wrote? file.rawRead(address[0 .. desiredLength]) should do what you want.
Apr 07 2021
parent reply tcak <tcak gmail.com> writes:
On Wednesday, 7 April 2021 at 12:50:01 UTC, Adam D. Ruppe wrote:
 On Wednesday, 7 April 2021 at 11:42:56 UTC, tcak wrote:
 There is rawRead, but it takes an array as parameter, which 
 causes a dirty looking code with cast etc!
What did you wrote? file.rawRead(address[0 .. desiredLength]) should do what you want.
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.
Apr 07 2021
parent Adam D. Ruppe <destructionator gmail.com> writes:
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.
Apr 07 2021