106_files.zig 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // if is any other unexpected error we just propagate it through
  38. else => return e,
  39. };
  40. // then we'll try to open our freshly created directory
  41. // wait a minute
  42. // opening a directory might fail!
  43. // what should we do here?
  44. var output_dir: std.fs.Dir = cwd.openDir("output", .{});
  45. defer output_dir.close();
  46. // we try to open the file `zigling.txt`,
  47. // and propagate the error up if there are any errors
  48. const file: std.fs.File = try output_dir.createFile("zigling.txt", .{});
  49. // it is a good habit to close a file after you are done with it
  50. // so that other programs can read it and prevent data corruption
  51. // but here we are not yet done writing to the file
  52. // if only there were a keyword in zig that
  53. // allows you "defer" code execute to the end of scope...
  54. file.close();
  55. // !you are not allowed to switch these two lines above the file closing line!
  56. const byte_written = try file.write("It's zigling time!");
  57. std.debug.print("Successfully wrote {d} bytes.\n", .{byte_written});
  58. }
  59. // to check if you actually write to the file, you can either,
  60. // 1. open the file on your text editor, or
  61. // 2. print the content of the file in the console with the following command
  62. // >> cat ./output/zigling.txt
  63. //
  64. //
  65. // More on Creating files
  66. //
  67. // notice in:
  68. // ... try output_dir.createFile("zigling.txt", .{});
  69. // ^^^
  70. // we passed this anonymous struct to the function call
  71. //
  72. // this is the struct `CreateFlag` with default fields
  73. // {
  74. // read: bool = false,
  75. // truncate: bool = true,
  76. // exclusive: bool = false,
  77. // lock: Lock = .none,
  78. // lock_nonblocking: bool = false,
  79. // mode: Mode = default_mode
  80. // }
  81. //
  82. // Question:
  83. // - what should you do if you want to also read the file after opening it?
  84. // - go to documentation of the struct `std.fs.Dir` here
  85. // https://ziglang.org/documentation/master/std/#std.fs.Dir
  86. // - can you find a function for opening a file? how about deleting a file?
  87. // - what kind of options can you use with those functions?