summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2024-12-18 23:05:29 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2024-12-18 23:05:29 +0100
commit3fbe24e6cde0e423f603c41ec06da736bf1a275b (patch)
tree303d81631f315c71ad116237e5485898bdc569f0
parent32bb63d22e243d4e1e0998f97b5528ea86e1ae78 (diff)
downloadzig-playground-3fbe24e6cde0e423f603c41ec06da736bf1a275b.tar.gz
zig-playground-3fbe24e6cde0e423f603c41ec06da736bf1a275b.zip
fileio: minimal example
-rw-r--r--example-fileio/Makefile6
-rw-r--r--example-fileio/file.zig23
2 files changed, 29 insertions, 0 deletions
diff --git a/example-fileio/Makefile b/example-fileio/Makefile
new file mode 100644
index 0000000..d3b22bd
--- /dev/null
+++ b/example-fileio/Makefile
@@ -0,0 +1,6 @@
+run:
+ zig run file.zig
+ od -tx1 -tc moose
+
+clean:
+ $(RM) moose
diff --git a/example-fileio/file.zig b/example-fileio/file.zig
new file mode 100644
index 0000000..41f7739
--- /dev/null
+++ b/example-fileio/file.zig
@@ -0,0 +1,23 @@
+const std = @import("std");
+
+const Foo = extern struct {
+ x: u8,
+ y: u8,
+ z: u8,
+};
+
+pub fn main() !void {
+ const f = std.fs.cwd().createFile("moose", .{ .truncate = true }) catch unreachable;
+
+ // Write raw byte slice.
+ try f.writeAll(&[_]u8{ 1, 2, 3, 4 });
+ // Write raw byte slice (ascii chars).
+ try f.writeAll("abcd");
+
+ // Format printing.
+ const w = f.writer();
+ try w.print("{}-{}", .{ 8, 9 });
+
+ // Write struct to file.
+ try w.writeStruct(Foo{ .x = 0xaa, .y = 0xbb, .z = 0xcc });
+}