Skip to content

Commit

Permalink
fix: file existence
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Jan 4, 2025
1 parent 0f9a740 commit cf13c5c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion book-src/01-04-file-exists.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Check file existence

In this example, we use `panic` to ensure `build.zig` should exist.
In this example, `access` is utilized to verify file existence; however, for it to function correctly, one must specifically check for the `FileNotFound` error type.

```zig
{{#include ../src/01-04.zig}}
```
12 changes: 7 additions & 5 deletions src/01-04.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! Check file existence
//! Test file/directory existence

const std = @import("std");
const fs = std.fs;

pub fn main() void {
pub fn main() !void {
const filename = "build.zig";
fs.cwd().access(filename, .{}) catch {
std.debug.panic("{s} not exists", .{filename});
var found = true;
fs.cwd().access(filename, .{}) catch |e| switch (e) {
error.FileNotFound => found = false,
else => return e,
};

std.debug.print("{s} exists", .{filename});
std.debug.assert(found);
}

0 comments on commit cf13c5c

Please sign in to comment.