Reputation: 5376
I tried to do lseek() on a file which is opened in write mode as below. But it is not working as expected.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
main()
{
int fd = open("/local/sandbox/.C/mytest", O_WRONLY | O_APPEND);
if(fd == -1)
{
printf("\nFailed to opne file in write mode\n");
return -1;
}
lseek(fd, 2, SEEK_SET);
write(fd, "OK", 2);
write(fd, "AAAAAAAA", 3);
close(fd);
}
'mytest' file is already existed with the content "Hi, How are you". I thought after executing the program, my test will contain 'HiOKAAA, How are you". But instead it is writing "OKAAA" at the end. Is it because of O_APPEND flag? But even, I am using lseek() to change the file offset to '2' only. Can any one please let me know why it is failing?
Upvotes: 0
Views: 362