gpt-4-0314· OpenAI
Arena Human Preference 55K
Show how to read a file line by line in Zig.
- User
Show how to read a file line by line in Zig.
- Assistant
In Zig, you can read a file line by line by using the
std.fs.Filestruct and thereadLinefunction. Here's an example of how to do it:zigconst std = @import("std"); const fs = std.fs; pub fn main() !void { const allocator = std.heap.page_allocator; try readLines("example.txt", allocator); } fn readLines(path: []const u8, allocator: *std.mem.Allocator) !void { const file = try fs.cwd().openFile(path, .{}); defer file.close(); var buf: [1024]u8 = undefined; var line: []u8 = &buf; while (try file.reader().readLine(&line)) |line_read| { const line_str = try std.mem.sliceTo(allocator, u8, line_read); std.debug.print("Line: {}\n", .{line_str}); } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.