I’m not sure if this is intended behavior or if it’s a bug. The function std.fs.File.write() does has different behavior when that file is read previously.
when I run this code multiple times.
var file = try std.fs.cwd().openFile("text.txt", .{ .mode = .read_write });
defer file.close();
_ = try file.write("Some data\n");
that string will be written to the file only once.
However, when I run this code multiple times
var file = try std.fs.cwd().openFile("text.txt", .{ .mode = .read_write });
defer file.close();
var buffer: [64]u8 = undefined;
_ = try file.readAll(&buffer);
_ = try file.write("Some data\n");
that string appears on that file multiple times.
The function that reads the file moves the writer to the end of the file. When the data is written without reading, it overwrites that data over the file content without clearing it.
the zig version I’m using is 0.13.0.
If this is intentional, how to clear the old file content before writing the new one?