Added saving of struct to file in Zig and reading it back

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2024-09-19 14:01:34 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2024-09-19 14:01:34 +0200
Commit c7cd286be9d4ede3129a309e2668686be4646f07 (patch)
-rw-r--r-- README.md 3
-rw-r--r-- zig-struct-bin/Makefile 2
-rw-r--r-- zig-struct-bin/main.zig 35
-rw-r--r-- zig-struct-bin/out.bin bin 0 B -> 8 B
-rw-r--r-- zig-struct-json/Makefile 0
-rw-r--r-- zig-struct-json/player.json 0
-rw-r--r-- zig-struct-json/player.zig 0
-rw-r--r-- zig-struct-json/read.zig 0
-rw-r--r-- zig-struct-json/write.zig 0
9 files changed, 39 insertions, 1 deletions
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/Makefile b/zig-struct-bin/Makefile
  
1
default:
  
2
	zig test main.zig
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 {}
diff --git a/zig-struct-bin/out.bin b/zig-struct-bin/out.bin
diff --git a/zig-struct-json/Makefile b/zig-struct-json/Makefile
...
diff --git a/zig-struct-json/player.json b/zig-struct-json/player.json
...
diff --git a/zig-struct-json/player.zig b/zig-struct-json/player.zig
...
diff --git a/zig-struct-json/read.zig b/zig-struct-json/read.zig
...
diff --git a/zig-struct-json/write.zig b/zig-struct-json/write.zig
...