106_files.zig 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Until now, we've only been printing our output in the console,
  3. // which is good enough for fighting alien and hermit bookkeeping.
  4. //
  5. // However, many other task require some interaction with the file system,
  6. // which is the underlying structure for organizing files on your computer.
  7. //
  8. // The File System provide a hierarchical structure for storing files
  9. // by organizing files into directories, which hold files and other directories,
  10. // thus creating a tree structure for navigating.
  11. //
  12. // Fortunately, zig standard library provide a simple api for interacting
  13. // with the file system, see the detail documentation here
  14. //
  15. // https://ziglang.org/documentation/master/std/#std.fs
  16. //
  17. // In this exercise, we'll try to
  18. // - create a new directory
  19. // - open a file in the directory
  20. // - write to the file.
  21. //
  22. // import std as always
  23. const std = @import("std");
  24. pub fn main() !void {
  25. // first we get the current working directory
  26. const cwd: std.fs.Dir = std.fs.cwd();
  27. // then we'll try to make a new directory /output/
  28. // to put our output files.
  29. cwd.makeDir("output") catch |e| switch (e) {
  30. // there are chance you might want to run this
  31. // program more than once and the path might already
  32. // been created, so we'll have to handle this error
  33. // by doing nothing
  34. //
  35. // we want to catch error.PathAlreadyExists and do nothing
  36. ??? => {
  37. ???
  38. },
  39. // if is any other unexpected error we just propagate it through
  40. else => return e,
  41. };
  42. // then we'll try to open our freshly created directory
  43. // wait a minute
  44. // opening a directory might fail!
  45. // what should we do here?
  46. const output_dir: std.fs.Dir = cwd.openDir("output", .{});
  47. // we try to open the file `zigling.txt`,
  48. // and propagate the error up if there are any errors
  49. const file: std.fs.File = try output_dir.createFile("zigling.txt", .{});
  50. // it is a good habit to close a file after you are done with
  51. // so that other program can read it and prevent data corruption
  52. // but here we are not yet done writing to the file
  53. // if only there are a keyword in zig that
  54. // allow you "defer" code execute to the end of scope...
  55. file.close();
  56. // !you are not allow to switch this two lines to before file closing line!
  57. const byte_written = try file.write("It's zigling time!");
  58. std.debug.print("succefully wrote {} bytes.\n", .{byte_written});
  59. }
  60. // to check if you actually write to the file, you can either,
  61. // 1. open the file on your text editor, or
  62. // 2. print the content of the file in the console with command
  63. // >> cat ./output/zigling.txt
  64. //
  65. //
  66. // More on Creating files
  67. //
  68. // notice in:
  69. // ... try output_dir.createFile("zigling.txt", .{});
  70. // ^^^
  71. // we passed this anonymous struct to the function call
  72. //
  73. // this is the struct `CreateFlag` with default fields
  74. // {
  75. // read: bool = false,
  76. // truncate: bool = true,
  77. // exclusive: bool = false,
  78. // lock: Lock = .none,
  79. // lock_nonblocking: bool = false,
  80. // mode: Mode = default_mode
  81. // }
  82. //
  83. // Question:
  84. // - what should you do if you want to also read the file after opening it?
  85. // - go to documentation of the struct `std.fs.Dir` here
  86. // https://ziglang.org/documentation/master/std/#std.fs.Dir
  87. // - can you find a function for opening a file? how about deleting a file?
  88. // - what kind of option can you uses with those function?