File input/output: Difference between revisions
→{{header|Zig}}: Truly read file's content in intermediate variable (on heap)
imported>Brie (Add Nu) |
(→{{header|Zig}}: Truly read file's content in intermediate variable (on heap)) |
||
Line 3,968:
=={{header|Zig}}==
'''Works with:''' 0.10.x, 0.11.x, 0.12.0-dev.1357+10d03acdb
<syntaxhighlight lang="zig">const std = @import("std");
pub fn main() (error{OutOfMemory} || std.fs.File.OpenError || std.fs.File.ReadError || std.fs.File.WriteError)!void {
var
defer
const allocator = gpa.allocator();
var out = try std.fs.cwd().openFile("output.txt", .{ .mode = .write_only });▼
defer out.close();▼
▲ var
var
// Restrict input_file's size to "up to 10 MiB".
var input_file_content = try input_file.readToEndAlloc(allocator, 10 * 1024 * 1024);
defer allocator.free(input_file_content);
try output_file.writeAll(input_file_content);
return;
}</syntaxhighlight>
|