diff --git a/README.md b/README.md index fe1b93103a56cdd0abf7fcbe63958af8229c7da5..22944a71a43ddaaecbd01b1cb45439ffcaee9b40 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ | [c-signals](./c-signals) | clang-17 | Uses SIGUSR1 and SIGUSR2 as IPC mechanism. | | [c-structs](./c-structs) | clang-17 | Saves and reads structs in/from binary files. | | [zig-c-interop](./zig-c-interop) | zig-0.11.0 | Uses functions written in C from Zig code. | | [zig-ppm](./zig-ppm) | zig-0.11.0 | Creates an image with random pixels in PPM image format. | -| [zig-structs](./zig-structs) | zig-0.11.0 | Serialization of a struct into JSON and then reading it back. | +| [zig-struct-json](./zig-struct-json) | zig-0.11.0 | Serialization of a struct into JSON and then reading it back. | | [zig-telnet](./zig-telnet) | zig-0.11.0 | Connects to Redis server like it is a basic telnet server. | | [zig-x11](./zig-x11) | zig-0.11.0 | Uses X11 to create a basic window without any bindings needed. | | [zig-http](./zig-http) | zig-0.11.0 | Basic example of a HTTP 1.1 server without any routing etc. | @@ -34,6 +34,7 @@ | [zig-wad](./zig-wad) | zig-0.11.0 | Reads doom.wad and extracts the identification header. | | [zig-os-props](./zig-os-props) | zig-0.11.0 | Detects properties of the target operating system. | | [zig-tlv-encoding](./zig-tlv-encoding) | zig-0.13.0 | Naive implementation of TLV encoding in Zig. | | [zig-embed](./zig-embed) | zig-0.13.0 | Embedding external resources in compiled binary. | +| [zig-struct-bin](./zig-struct-bin) | zig-0.13.0 | Save a struct into binary file and then reading it back. | ## License diff --git a/zig-struct-bin/Makefile b/zig-struct-bin/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..98beea6ce4d57f545c92959f91b3774a6d6fd290 --- /dev/null +++ b/zig-struct-bin/Makefile @@ -0,0 +1,2 @@ +default: + zig test main.zig diff --git a/zig-struct-bin/main.zig b/zig-struct-bin/main.zig new file mode 100644 index 0000000000000000000000000000000000000000..41932422b575b9b632a72c3f02092a3d1a1a5208 --- /dev/null +++ b/zig-struct-bin/main.zig @@ -0,0 +1,35 @@ +const std = @import("std"); + +const Sample = packed struct { + type: u16, + machine: u16, + version: u32, +}; + +test "write" { + const s = Sample{ + .type = 54, + .machine = 72, + .version = 132, + }; + + std.debug.print("{}\n", .{s}); + + var file = try std.fs.cwd().createFile("out.bin", .{}); + defer file.close(); + + try file.writeAll(std.mem.asBytes(&s)); +} + +test "read" { + var file = try std.fs.cwd().openFile("./out.bin", .{}); + defer file.close(); + + var buffer: [8]u8 = undefined; + _ = try file.read(buffer[0..]); + + const s: *Sample = @ptrCast(@alignCast(&buffer)); + std.debug.print("{}\n", .{s}); +} + +pub fn main() void {} diff --git a/zig-struct-bin/out.bin b/zig-struct-bin/out.bin new file mode 100644 index 0000000000000000000000000000000000000000..44ae9d85adffb45f7bbfa68fc903ae365623745c Binary files /dev/null and b/zig-struct-bin/out.bin differ diff --git a/zig-structs/Makefile b/zig-struct-json/Makefile rename from zig-structs/Makefile rename to zig-struct-json/Makefile diff --git a/zig-structs/player.json b/zig-struct-json/player.json rename from zig-structs/player.json rename to zig-struct-json/player.json diff --git a/zig-structs/player.zig b/zig-struct-json/player.zig rename from zig-structs/player.zig rename to zig-struct-json/player.zig diff --git a/zig-structs/read.zig b/zig-struct-json/read.zig rename from zig-structs/read.zig rename to zig-struct-json/read.zig diff --git a/zig-structs/write.zig b/zig-struct-json/write.zig rename from zig-structs/write.zig rename to zig-struct-json/write.zig