Last active
August 11, 2024 04:44
-
-
Save komuw/98111edb30262f0c34d93e3aa7f537ed to your computer and use it in GitHub Desktop.
read a file in 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
const std = @import("std"); | |
const os = std.os; | |
const warn = std.debug.warn; | |
pub fn main() !void { | |
var file = try os.File.openRead("/path/to/file.txt"); | |
defer file.close(); | |
const file_size = try file.getEndPos(); | |
// why cant I use? | |
// var buffer: [file_size]u8 = undefined; | |
// ie, I only want to create a buffer that is same size as | |
// the file been read. | |
var buffer: [1024 * 4]u8 = undefined; | |
const bytes_read = try file.read(buffer[0..buffer.len]); | |
warn("{}", buffer[0..bytes_read]); | |
} |
answer on IRC: file_size is a runtime value, array lengths are by definition comptime known
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why cant I use?
ie, I only want to create a buffer that is same size as the file been read.