summaryrefslogtreecommitdiff
path: root/example-fileio
diff options
context:
space:
mode:
Diffstat (limited to 'example-fileio')
-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 });
+}