Why isnot line appended at the end of file?

fn appendToHitFile(issue_number: []const u8, hit_numbers: []const u8) anyerror!void { 
   const file = try
 fs.cwd().openFile(SSQHITNUM,.
{.mode=.read_write});
 defer file.close();
    
var buffer: [1024]u8 = undefined;
var reader = file.reader(&buffer);
while(reader.interface.takeDelimiterExclusive('\n')) |line| {                          const index = std.mem.indexOf(u8, line, issue_number);
if (index != null) {
return; 
}
}
else |err| switch (err) {                   error.EndOfStream => {},
else => |e| return e,
}
var writer = file.writer(&buffer);
const endPos = try
 file.getEndPos();          
 try file.seekTo(endPos);
try writer.interface.print("{s} " .{issue_number});

for (hit_numbers) |byte| {
try writer.interface.print("{d} ",.{byte});                                     }
try writer.interface.print("\n",.{});
// After writing, flush the buffer  
try writer.interface.flush(); 
try file.sync();   
}

Any suggestion is appreciated !

By default, the file reader/writer use positional syscalls, meaning they track their own position instead of relying on the position associated with the file handle.

You updated the position of the file handle, not the writer’s position. You can change the mode of the reader/writer to use the file handles position, that is called streaming mode.

But a better solution is to use writer.seekTo instead, it will update its own position or the file handles position, whichever is appropriate for what mode it is in.

2 Likes