Jump to content

File input/output: Difference between revisions

add Zig example
(add Zig example)
Line 3,772:
In these last two, the implicit buffer (whose size if given by the numbers) is visible inside the pump. Consider this example, which converts input to upper case text on its way to output (the Data() is optional; using it chunks, without, lines):
<lang zkl>File("input.txt").pump(Data(),File("output.txt","w"),"text","toUpper");</lang>
 
=={{header|Zig}}==
<lang zig>const std = @import("std");
 
pub fn main() !void {
var in = try std.fs.cwd().openFile("input.txt", .{});
defer in.close();
var out = try std.fs.cwd().openFile("output.txt", .{ .mode = .write_only });
defer out.close();
var file_reader = in.reader();
var file_writer = out.writer();
var buf: [100]u8 = undefined;
var read: usize = 1;
while (read > 0) {
read = try file_reader.readAll(&buf);
try file_writer.writeAll(buf[0..read]);
}
}</lang>
 
{{omit from|HTML}}
37

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.