So I'm trying to learn C file IO operations from a snippet from the university. My problem is that SEEK_END
doesn't work as I expect it to work.
Let met give you more details:
input.txt:
abcd-abcd-abcd
code:
int fd, fdr, l1, l2, wb1, wb2;
char buf[25];
fd = open("input.txt", O_WRONLY);
fdr = open("input.txt", O_RDONLY);
l1 = lseek(fd, -3, SEEK_END);
wb1 = write(fd, "xy", 2);
l2 = lseek(fd, 4, SEEK_SET);
write(fd, "12", 2);
lseek(fdr, 0, SEEK_SET);
wb2 = read(fdr, buf, 20);
write(1, buf, wb2);
My problem is writing "xy". I expect the output to be
abcd12bcd-axyd
Instead it is
abcd12bcd-abcd
Why the "xy" is not written?