Last active
January 12, 2020 18:02
-
-
Save wjlroe/4042b3a6307c02cab6896a22b3d4f8a4 to your computer and use it in GitHub Desktop.
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
const std = @import("std"); | |
pub fn main() !void { | |
const allocator = std.heap.c_allocator; | |
const cwd = try std.process.getCwdAlloc(allocator); | |
defer allocator.free(cwd); | |
var paths = std.ArrayList([]const u8).init(allocator); | |
{ | |
var dir = try std.fs.Dir.open(allocator, cwd); | |
defer dir.close(); | |
// here, all the paths are fine | |
while (try dir.next()) |entry| { | |
std.debug.warn("appending file: '{}'\n", entry.name); | |
try paths.append(entry.name); | |
} | |
} | |
std.debug.warn("\n"); | |
const paths_done = paths.toSliceConst(); | |
// but here, the first one will be blank/empty | |
// something to do with running the defer on line 12 (dir.close()) | |
for (paths_done) |path| { | |
std.debug.warn("(after dir.close() has run), file: '{}'\n", path); | |
} | |
} | |
// Run with: `zig run -l c lost_path.zig` | |
// | |
// This will output (on Zig 0.5.0, when cloned from gist.github.com, i.e. when there's a `.git` directory present): | |
// | |
// appending file: '.git' | |
// appending file: 'lost_path.zig' | |
// | |
// (after dir.close() has run), file: '' | |
// (after dir.close() has run), file: 'lost_path.zig' | |
// | |
// === End of output === | |
// | |
// ^ The first path in the slice always ends up as an empty string. This | |
// happens seemingly as a result of running `dir.close()` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment