Hey, hey.
I have been banging my head how to map a buffer to a struct like you do in C with fread. I have been trying to use mem.copy, doing pointer casting etc. without any luck. Also, since the language is still in development, a lot of examples don’t work with version 0.13.
There must be an intuitive straightforward way of doing this, and I’m just not seeing it and we all know the tunnel vision you get when something just isn’t working the way you want it to
The code below reads the first 64 bytes of elf file, and I would like to map this onto a Elf64ExecutionHeader struct. The file is being read, and I can print it out without any issues.
const std = @import("std");
const Elf64ExecutionHeader = struct {
ident: [16]u8,
type: u16,
machine: u16,
version: u32,
entry: u64,
phoff: u64,
shoff: u64,
flags: u32,
ehsize: u16,
phentsize: u16,
phnum: u16,
shentsize: u16,
shnum: u16,
shstrndx: u16,
};
pub fn main() !void {
var file = try std.fs.cwd().openFile("./elf", .{});
defer file.close();
var buffer: [64]u8 = undefined;
_ = try file.read(buffer[0..]);
std.debug.print("{s}\n", .{buffer[0..]});
}
Any help would be appreciated. Finding up-to-date resources on the interwebs is quite challenging.