From 4192a7fea4f7f43b0e37121d1f86b7a2d14ee089 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 21 Jun 2024 09:21:03 -0400 Subject: [PATCH] 106-107 completed --- exercises/106_files.zig | 6 +++--- exercises/107_files2.zig | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/106_files.zig b/exercises/106_files.zig index f5fd1ac..1c985c7 100644 --- a/exercises/106_files.zig +++ b/exercises/106_files.zig @@ -35,7 +35,7 @@ pub fn main() !void { // by doing nothing // // we want to catch error.PathAlreadyExists and do nothing - ??? => {}, + error.PathAlreadyExists => {}, // if there's any other unexpected error we just propagate it through else => return e, }; @@ -44,7 +44,7 @@ pub fn main() !void { // wait a minute... // opening a directory might fail! // what should we do here? - var output_dir: std.fs.Dir = cwd.openDir("output", .{}); + var output_dir: std.fs.Dir = try cwd.openDir("output", .{}); defer output_dir.close(); // we try to open the file `zigling.txt`, @@ -55,7 +55,7 @@ pub fn main() !void { // but here we are not yet done writing to the file // if only there were a keyword in Zig that // allowed you to "defer" code execution to the end of the scope... - file.close(); + defer file.close(); // you are not allowed to move these two lines above the file closing line! const byte_written = try file.write("It's zigling time!"); diff --git a/exercises/107_files2.zig b/exercises/107_files2.zig index 45e12f5..bf8c0b8 100644 --- a/exercises/107_files2.zig +++ b/exercises/107_files2.zig @@ -33,7 +33,7 @@ pub fn main() !void { // initalize an array of u8 with all letter 'A' // we need to pick the size of the array, 64 seems like a good number // fix the initalization below - var content = ['A']*64; + var content = [_]u8{'A'} ** 64; // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` std.debug.print("{s}\n", .{content}); @@ -41,12 +41,12 @@ pub fn main() !void { // can you go here to find a way to read the content? // https://ziglang.org/documentation/master/std/#std.fs.File // hint: you might find two answers that are both vaild in this case - const bytes_read = zig_read_the_file_or_i_will_fight_you(&content); + const bytes_read = try file.readAll(&content); // Woah, too screamy. I know you're excited for zigling time but tone it down a bit. // Can you print only what we read from the file? std.debug.print("Successfully Read {d} bytes: {s}\n", .{ bytes_read, - content, // change this line only + content[0..bytes_read], // change this line only }); }