|
diff --git a/README.md b/README.md
|
| ... |
| 24 |
| [c-structs](./c-structs) | clang-17 | Saves and reads structs in/from binary files. | |
24 |
| [c-structs](./c-structs) | clang-17 | Saves and reads structs in/from binary files. | |
| 25 |
| [zig-c-interop](./zig-c-interop) | zig-0.11.0 | Uses functions written in C from Zig code. | |
25 |
| [zig-c-interop](./zig-c-interop) | zig-0.11.0 | Uses functions written in C from Zig code. | |
| 26 |
| [zig-ppm](./zig-ppm) | zig-0.11.0 | Creates an image with random pixels in PPM image format. | |
26 |
| [zig-ppm](./zig-ppm) | zig-0.11.0 | Creates an image with random pixels in PPM image format. | |
| 27 |
| [zig-structs](./zig-structs) | zig-0.11.0 | Serialization of a struct into JSON and then reading it back. | |
27 |
| [zig-struct-json](./zig-struct-json) | zig-0.11.0 | Serialization of a struct into JSON and then reading it back. | |
| 28 |
| [zig-telnet](./zig-telnet) | zig-0.11.0 | Connects to Redis server like it is a basic telnet server. | |
28 |
| [zig-telnet](./zig-telnet) | zig-0.11.0 | Connects to Redis server like it is a basic telnet server. | |
| 29 |
| [zig-x11](./zig-x11) | zig-0.11.0 | Uses X11 to create a basic window without any bindings needed. | |
29 |
| [zig-x11](./zig-x11) | zig-0.11.0 | Uses X11 to create a basic window without any bindings needed. | |
| 30 |
| [zig-http](./zig-http) | zig-0.11.0 | Basic example of a HTTP 1.1 server without any routing etc. | |
30 |
| [zig-http](./zig-http) | zig-0.11.0 | Basic example of a HTTP 1.1 server without any routing etc. | |
| ... |
| 34 |
| [zig-os-props](./zig-os-props) | zig-0.11.0 | Detects properties of the target operating system. | |
34 |
| [zig-os-props](./zig-os-props) | zig-0.11.0 | Detects properties of the target operating system. | |
| 35 |
| [zig-tlv-encoding](./zig-tlv-encoding) | zig-0.13.0 | Naive implementation of TLV encoding in Zig. | |
35 |
| [zig-tlv-encoding](./zig-tlv-encoding) | zig-0.13.0 | Naive implementation of TLV encoding in Zig. | |
| 36 |
| [zig-embed](./zig-embed) | zig-0.13.0 | Embedding external resources in compiled binary. | |
36 |
| [zig-embed](./zig-embed) | zig-0.13.0 | Embedding external resources in compiled binary. | |
|
|
37 |
| [zig-struct-bin](./zig-struct-bin) | zig-0.13.0 | Save a struct into binary file and then reading it back. | |
| 37 |
|
38 |
|
| 38 |
## License |
39 |
## License |
| 39 |
|
40 |
|
| ... |
|
diff --git a/zig-struct-bin/main.zig b/zig-struct-bin/main.zig
|
|
|
1 |
const std = @import("std"); |
|
|
2 |
|
|
|
3 |
const Sample = packed struct { |
|
|
4 |
type: u16, |
|
|
5 |
machine: u16, |
|
|
6 |
version: u32, |
|
|
7 |
}; |
|
|
8 |
|
|
|
9 |
test "write" { |
|
|
10 |
const s = Sample{ |
|
|
11 |
.type = 54, |
|
|
12 |
.machine = 72, |
|
|
13 |
.version = 132, |
|
|
14 |
}; |
|
|
15 |
|
|
|
16 |
std.debug.print("{}\n", .{s}); |
|
|
17 |
|
|
|
18 |
var file = try std.fs.cwd().createFile("out.bin", .{}); |
|
|
19 |
defer file.close(); |
|
|
20 |
|
|
|
21 |
try file.writeAll(std.mem.asBytes(&s)); |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
test "read" { |
|
|
25 |
var file = try std.fs.cwd().openFile("./out.bin", .{}); |
|
|
26 |
defer file.close(); |
|
|
27 |
|
|
|
28 |
var buffer: [8]u8 = undefined; |
|
|
29 |
_ = try file.read(buffer[0..]); |
|
|
30 |
|
|
|
31 |
const s: *Sample = @ptrCast(@alignCast(&buffer)); |
|
|
32 |
std.debug.print("{}\n", .{s}); |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
pub fn main() void {} |