tags:

views:

52

answers:

1

hello guys I want to rewrite a particular block in a file but its not working for me

For example if I want to rewrite to offset 4 of the file I used lseek(fd,4,SEEK_SET) and called write system call but its writing at the end of the file instead of at offset 4. Can someone please help me.

+3  A: 

Don't use O_APPEND. It will append everything to the end of the file, regardless of your seeking.

Use:

open("file.txt", O_RDWR);

You're assuming the file already exists, so I don't see why you would use O_CREAT.

Matthew Flaschen
@Matthew thank you very much. If I want to restart a file how to do it ?
mousey
You mean truncate it? You can use [`ftruncate`](http://opengroup.org/onlinepubs/007908799/xsh/ftruncate.html).
Matthew Flaschen
@Mathew If no it file is there it will create it right ?
mousey
@Matthew yeah thats what I wanted. Also can I remove some data in between two offsets ?
mousey