ziglings/exercises/106_files.zig

93 lines
3.5 KiB
Zig
Raw Normal View History

2024-03-24 13:11:33 -04:00
//
// Until now, we've only been printing our output in the console,
2024-06-12 03:52:33 -04:00
// which is good enough for fighting aliens and hermit bookkeeping.
2024-03-24 13:11:33 -04:00
//
2024-06-12 03:52:33 -04:00
// However, many other tasks require some interaction with the file system,
2024-03-24 13:11:33 -04:00
// which is the underlying structure for organizing files on your computer.
//
2024-06-12 03:52:33 -04:00
// The file system provides a hierarchical structure for storing files
// by organizing them into directories, which hold files and other directories,
// thus creating a tree structure that can be navigated.
2024-03-25 04:36:15 -04:00
//
2024-06-12 03:52:33 -04:00
// Fortunately, the Zig standard library provides a simple API for interacting
// with the file system, see the detail documentation here:
2024-03-24 13:11:33 -04:00
//
// https://ziglang.org/documentation/master/std/#std.fs
//
2024-06-12 03:52:33 -04:00
// In this exercise, we'll try to:
// - create a new directory,
// - open a file in the directory,
2024-03-24 13:11:33 -04:00
// - write to the file.
//
// import std as always
const std = @import("std");
pub fn main() !void {
// first we get the current working directory
const cwd: std.fs.Dir = std.fs.cwd();
// then we'll try to make a new directory /output/
2024-06-12 03:52:33 -04:00
// to store our output files.
2024-03-24 13:11:33 -04:00
cwd.makeDir("output") catch |e| switch (e) {
2024-06-08 13:20:30 -04:00
// there is a chance you might want to run this
2024-03-24 13:11:33 -04:00
// program more than once and the path might already
2024-06-12 03:52:33 -04:00
// have been created, so we'll have to handle this error
2024-03-24 13:11:33 -04:00
// by doing nothing
//
// we want to catch error.PathAlreadyExists and do nothing
2024-03-27 04:40:24 -04:00
??? => {},
2024-06-12 03:52:33 -04:00
// if there's any other unexpected error we just propagate it through
2024-03-24 13:11:33 -04:00
else => return e,
};
// then we'll try to open our freshly created directory
2024-06-12 03:52:33 -04:00
// wait a minute...
2024-03-24 13:11:33 -04:00
// opening a directory might fail!
// what should we do here?
2024-03-27 04:40:24 -04:00
var output_dir: std.fs.Dir = cwd.openDir("output", .{});
defer output_dir.close();
2024-03-24 13:11:33 -04:00
// we try to open the file `zigling.txt`,
2024-06-12 03:52:33 -04:00
// and propagate any error up
2024-03-24 13:11:33 -04:00
const file: std.fs.File = try output_dir.createFile("zigling.txt", .{});
2024-05-04 16:35:07 -04:00
// it is a good habit to close a file after you are done with it
// so that other programs can read it and prevent data corruption
2024-03-24 13:11:33 -04:00
// but here we are not yet done writing to the file
2024-06-12 03:52:33 -04:00
// if only there were a keyword in Zig that
// allowed you to "defer" code execution to the end of the scope...
2024-03-26 01:51:59 -04:00
file.close();
2024-03-24 13:11:33 -04:00
2024-06-12 03:52:33 -04:00
// you are not allowed to move these two lines above the file closing line!
2024-03-24 13:11:33 -04:00
const byte_written = try file.write("It's zigling time!");
2024-03-27 04:53:19 -04:00
std.debug.print("Successfully wrote {d} bytes.\n", .{byte_written});
2024-03-24 13:11:33 -04:00
}
// to check if you actually write to the file, you can either,
2024-06-12 03:52:33 -04:00
// 1. open the file in your text editor, or
2024-05-04 16:35:07 -04:00
// 2. print the content of the file in the console with the following command
2024-03-24 13:11:33 -04:00
// >> cat ./output/zigling.txt
//
2024-03-25 04:36:15 -04:00
//
2024-03-24 13:11:33 -04:00
// More on Creating files
2024-03-25 04:36:15 -04:00
//
2024-03-24 13:11:33 -04:00
// notice in:
// ... try output_dir.createFile("zigling.txt", .{});
// ^^^
// we passed this anonymous struct to the function call
2024-03-25 04:36:15 -04:00
//
2024-03-24 13:11:33 -04:00
// this is the struct `CreateFlag` with default fields
// {
// read: bool = false,
// truncate: bool = true,
// exclusive: bool = false,
// lock: Lock = .none,
// lock_nonblocking: bool = false,
// mode: Mode = default_mode
// }
2024-03-25 04:36:15 -04:00
//
2024-03-24 13:11:33 -04:00
// Question:
// - what should you do if you want to also read the file after opening it?
2024-06-12 03:52:33 -04:00
// - go to the documentation of the struct `std.fs.Dir` here:
2024-03-24 13:11:33 -04:00
// https://ziglang.org/documentation/master/std/#std.fs.Dir
// - can you find a function for opening a file? how about deleting a file?
2024-05-04 16:35:07 -04:00
// - what kind of options can you use with those functions?