summaryrefslogtreecommitdiff
path: root/example-fileio/file.zig
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 /example-fileio/file.zig
parent32bb63d22e243d4e1e0998f97b5528ea86e1ae78 (diff)
downloadzig-playground-3fbe24e6cde0e423f603c41ec06da736bf1a275b.tar.gz
zig-playground-3fbe24e6cde0e423f603c41ec06da736bf1a275b.zip
fileio: minimal example
Diffstat (limited to 'example-fileio/file.zig')
-rw-r--r--example-fileio/file.zig23
1 files changed, 23 insertions, 0 deletions
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 });
+}