From 3fbe24e6cde0e423f603c41ec06da736bf1a275b Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 18 Dec 2024 23:05:29 +0100 Subject: fileio: minimal example --- example-fileio/Makefile | 6 ++++++ example-fileio/file.zig | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 example-fileio/Makefile create mode 100644 example-fileio/file.zig 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 }); +} -- cgit v1.2.3