I have a file descriptor, now I want to write n bytes to that file replacing n bytes of original data (so the total file size won't change), like in replace mode of a word processor. Any convenient way to achieve this?
Just write
to the fd. You might need to read
up to the right position before or lseek
or lseek64
directly to the right position.
You must be sure that the fd is writable. You need to check open
for that. open
must have been called with O_WRONLY
or O_RDWR
but without O_TRUNC
which would have truncated the file to zero length.
Note: write
to a file only changes (increases) the file size if you are writing over the end of the file. If you are writing right into the file it's size does not change.