digitalmars.D.learn - Read X many bytes from File to address Y
- tcak (9/9) Apr 07 2021 I am talking about std.file.File.
- Adam D. Ruppe (4/6) Apr 07 2021 What did you wrote?
- tcak (5/11) Apr 07 2021 Well, I have a struct, that is defined as a variable already. I
- Adam D. Ruppe (7/11) Apr 07 2021 file.rawRead((cast(ubyte*) &your_struct)[0 .. your_length]);
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
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
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: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.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
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