-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f9a740
commit cf13c5c
Showing
2 changed files
with
9 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |