Thread overview
Read X many bytes from File to address Y
Apr 07, 2021
tcak
Apr 07, 2021
Adam D. Ruppe
Apr 07, 2021
tcak
Apr 07, 2021
Adam D. Ruppe
April 07, 2021

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!

April 07, 2021

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.

April 07, 2021

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.

April 07, 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.