106-107 completed

This commit is contained in:
Andrew Scott 2024-06-21 09:21:03 -04:00
parent 8a6106f82b
commit 4192a7fea4
Signed by: a
GPG key ID: 7CD5A5977E4931C1
2 changed files with 6 additions and 6 deletions

View file

@ -35,7 +35,7 @@ pub fn main() !void {
// by doing nothing // by doing nothing
// //
// we want to catch error.PathAlreadyExists and do nothing // we want to catch error.PathAlreadyExists and do nothing
??? => {}, error.PathAlreadyExists => {},
// if there's any other unexpected error we just propagate it through // if there's any other unexpected error we just propagate it through
else => return e, else => return e,
}; };
@ -44,7 +44,7 @@ pub fn main() !void {
// wait a minute... // wait a minute...
// opening a directory might fail! // opening a directory might fail!
// what should we do here? // 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(); defer output_dir.close();
// we try to open the file `zigling.txt`, // 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 // but here we are not yet done writing to the file
// if only there were a keyword in Zig that // if only there were a keyword in Zig that
// allowed you to "defer" code execution to the end of the scope... // 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! // you are not allowed to move these two lines above the file closing line!
const byte_written = try file.write("It's zigling time!"); const byte_written = try file.write("It's zigling time!");

View file

@ -33,7 +33,7 @@ pub fn main() !void {
// initalize an array of u8 with all letter 'A' // initalize an array of u8 with all letter 'A'
// we need to pick the size of the array, 64 seems like a good number // we need to pick the size of the array, 64 seems like a good number
// fix the initalization below // fix the initalization below
var content = ['A']*64; var content = [_]u8{'A'} ** 64;
// this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
std.debug.print("{s}\n", .{content}); 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? // can you go here to find a way to read the content?
// https://ziglang.org/documentation/master/std/#std.fs.File // https://ziglang.org/documentation/master/std/#std.fs.File
// hint: you might find two answers that are both vaild in this case // 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. // 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? // Can you print only what we read from the file?
std.debug.print("Successfully Read {d} bytes: {s}\n", .{ std.debug.print("Successfully Read {d} bytes: {s}\n", .{
bytes_read, bytes_read,
content, // change this line only content[0..bytes_read], // change this line only
}); });
} }