From caf255d6c4b3fac1b484c980530a72d91f255d23 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Wed, 18 Jan 2023 13:59:11 +0100 Subject: [PATCH] Code example for iterating on directory entries. --- zig-impl/misc/messing-with-files.zig | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 zig-impl/misc/messing-with-files.zig diff --git a/zig-impl/misc/messing-with-files.zig b/zig-impl/misc/messing-with-files.zig new file mode 100644 index 0000000..68fd5d2 --- /dev/null +++ b/zig-impl/misc/messing-with-files.zig @@ -0,0 +1,51 @@ +const std = @import("std"); +const testing = std.testing; +const net = std.net; +const fmt = std.fmt; +const mem = std.mem; +const print = std.debug.print; + + +fn print_current_dir() !void { + const buffer_size = 10000; + var buffer: [buffer_size]u8 = undefined; + var fba = std.heap.FixedBufferAllocator.init(&buffer); + var allocator = fba.allocator(); + + print("Print current directory\n", .{}); + var current_dir = try std.fs.cwd().openIterableDir(".", .{}); + var walker = try current_dir.walk(allocator); + while (try walker.next()) |entry| { + print("content: {s}\n", .{entry.basename}); + } + walker.deinit(); + print("DONE\n", .{}); +} + +fn add_line_in_file() !void { + var cwd = std.fs.cwd(); + var f = try cwd.createFile("some-file.log", .{.read = true}); + // defer f.close(); // closed in add_line_from_fd + + var writer = f.writer(); + + try writer.print("hello\n", .{}); + + try add_line_from_fd(f.handle); +} + +fn add_line_from_fd(fd: i32) !void { + // var f = std.fs.File {.handle = fd}; + // defer f.close(); + + _ = try std.os.write(fd, "hello this is another line\n"); + std.os.close(fd); +} + +pub fn main() !u8 { + // var path = "/tmp/.TEST_USOCK"; + // print("Opening the file '{s}'.\n", .{path}); + //try print_current_dir(); + try add_line_in_file(); + return 0; +}