106_files.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --- exercises/106_files.zig 2024-06-17 10:11:53.651439869 +0200
  2. +++ answers/106_files.zig 2024-06-17 10:21:50.697337653 +0200
  3. @@ -1,22 +1,22 @@
  4. //
  5. // Until now, we've only been printing our output in the console,
  6. -// which is good enough for fighting aliens and hermit bookkeeping.
  7. +// which is good enough for fighting alien and hermit bookkeeping.
  8. //
  9. -// However, many other tasks require some interaction with the file system,
  10. +// However, many other task require some interaction with the file system,
  11. // which is the underlying structure for organizing files on your computer.
  12. //
  13. -// The file system provides a hierarchical structure for storing files
  14. -// by organizing them into directories, which hold files and other directories,
  15. -// thus creating a tree structure that can be navigated.
  16. +// The File System provide a hierarchical structure for storing files
  17. +// by organizing files into directories, which hold files and other directories,
  18. +// thus creating a tree structure for navigating.
  19. //
  20. -// Fortunately, the Zig standard library provides a simple API for interacting
  21. -// with the file system, see the detail documentation here:
  22. +// Fortunately, zig standard library provide a simple api for interacting
  23. +// with the file system, see the detail documentation here
  24. //
  25. // https://ziglang.org/documentation/master/std/#std.fs
  26. //
  27. -// In this exercise, we'll try to:
  28. -// - create a new directory,
  29. -// - open a file in the directory,
  30. +// In this exercise, we'll try to
  31. +// - create a new directory
  32. +// - open a file in the directory
  33. // - write to the file.
  34. //
  35. // import std as always
  36. @@ -27,42 +27,42 @@
  37. const cwd: std.fs.Dir = std.fs.cwd();
  38. // then we'll try to make a new directory /output/
  39. - // to store our output files.
  40. + // to put our output files.
  41. cwd.makeDir("output") catch |e| switch (e) {
  42. - // there is a chance you might want to run this
  43. + // there are chance you might want to run this
  44. // program more than once and the path might already
  45. - // have been created, so we'll have to handle this error
  46. + // been created, so we'll have to handle this error
  47. // by doing nothing
  48. //
  49. // we want to catch error.PathAlreadyExists and do nothing
  50. - ??? => {},
  51. - // if there's any other unexpected error we just propagate it through
  52. + error.PathAlreadyExists => {},
  53. + // if is any other unexpected error we just propagate it through
  54. else => return e,
  55. };
  56. // then we'll try to open our freshly created directory
  57. - // wait a minute...
  58. + // wait a minute
  59. // opening a directory might fail!
  60. // what should we do here?
  61. - var output_dir: std.fs.Dir = cwd.openDir("output", .{});
  62. + var output_dir: std.fs.Dir = try cwd.openDir("output", .{});
  63. defer output_dir.close();
  64. // we try to open the file `zigling.txt`,
  65. - // and propagate any error up
  66. + // and propagate the error up if there are any errors
  67. const file: std.fs.File = try output_dir.createFile("zigling.txt", .{});
  68. // it is a good habit to close a file after you are done with it
  69. // so that other programs can read it and prevent data corruption
  70. // but here we are not yet done writing to the file
  71. - // if only there were a keyword in Zig that
  72. - // allowed you to "defer" code execution to the end of the scope...
  73. - file.close();
  74. + // if only there were a keyword in zig that
  75. + // allows you "defer" code execute to the end of scope...
  76. + defer file.close();
  77. - // you are not allowed to move these two lines above the file closing line!
  78. + // !you are not allowed to switch these two lines above the file closing line!
  79. const byte_written = try file.write("It's zigling time!");
  80. std.debug.print("Successfully wrote {d} bytes.\n", .{byte_written});
  81. }
  82. // to check if you actually write to the file, you can either,
  83. -// 1. open the file in your text editor, or
  84. +// 1. open the file on your text editor, or
  85. // 2. print the content of the file in the console with the following command
  86. // >> cat ./output/zigling.txt
  87. //
  88. @@ -86,7 +86,7 @@
  89. //
  90. // Question:
  91. // - what should you do if you want to also read the file after opening it?
  92. -// - go to the documentation of the struct `std.fs.Dir` here:
  93. +// - go to documentation of the struct `std.fs.Dir` here
  94. // https://ziglang.org/documentation/master/std/#std.fs.Dir
  95. // - can you find a function for opening a file? how about deleting a file?
  96. // - what kind of options can you use with those functions?