107_files2.patch 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. --- exercises/107_files2.zig 2024-06-17 10:11:53.651439869 +0200
  2. +++ answers/107_files2.zig 2024-06-17 10:21:50.700671057 +0200
  3. @@ -4,17 +4,17 @@
  4. // - create a file {project_root}/output/zigling.txt
  5. // with content `It's zigling time!`(18 byte total)
  6. //
  7. -// Now there's no point in writing to a file if we don't read from it, am I right?
  8. -// Let's write a program to read the content of the file that we just created.
  9. +// Now there no point in writing to a file if we don't read from it am I right?
  10. +// let's write a program to read the content of the file that we just created.
  11. //
  12. // I am assuming that you've created the appropriate files for this to work.
  13. //
  14. -// Alright, bud, lean in close. Here's the game plan.
  15. +// Alright, bud, lean in close here's the game plan.
  16. // - First, we open the {project_root}/output/ directory
  17. // - Secondly, we open file `zigling.txt` in that directory
  18. -// - Then, we initalize an array of characters with all letter 'A', and print it
  19. -// - After that, we read the content of the file into the array
  20. -// - Finally, we print out the content we just read
  21. +// - then, we initalize an array of characters with all letter 'A', and print it
  22. +// - After that, we read the content of the file to the array
  23. +// - Finally, we print out the read content
  24. const std = @import("std");
  25. @@ -30,23 +30,23 @@
  26. const file = try output_dir.openFile("zigling.txt", .{});
  27. defer file.close();
  28. - // initalize an array of u8 with all letter 'A'
  29. - // we need to pick the size of the array, 64 seems like a good number
  30. + // initalize an array of u8 with all letter 'A'.
  31. + // we need to pick the size of the array, 64 seems like a good number.
  32. // fix the initalization below
  33. - var content = ['A']*64;
  34. + var content = [_]u8{'A'} ** 64;
  35. // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
  36. std.debug.print("{s}\n", .{content});
  37. // okay, seems like a threat of violence is not the answer in this case
  38. - // can you go here to find a way to read the content?
  39. + // can you go here to find a way to read the content ?
  40. // https://ziglang.org/documentation/master/std/#std.fs.File
  41. // hint: you might find two answers that are both vaild in this case
  42. - const bytes_read = zig_read_the_file_or_i_will_fight_you(&content);
  43. + const bytes_read = try file.read(&content);
  44. - // Woah, too screamy. I know you're excited for zigling time but tone it down a bit.
  45. - // Can you print only what we read from the file?
  46. + // Woah, too screamy, I know you're excited for zigling time but tone it down a bit
  47. + // Can you print only what we read from the file ?
  48. std.debug.print("Successfully Read {d} bytes: {s}\n", .{
  49. bytes_read,
  50. - content, // change this line only
  51. + content[0..bytes_read], // change this line only
  52. });
  53. }