Search code examples
clinuxfilebufferfwrite

In Linux/C programming, using the function write moves the indicator on the buffer too?


If i use a text file as a buffer, calling:

write(fd1, *buffer, count)
write(fd2, *buffer, count)

will write the same datas on both fd1 and fd2 from buffer or the write on fd2 will start from the point of the buffer where the write on fd1 finished?

(lurking a lot here, first time asking something, sorry if i did/wrote something wrong)


Solution

  • Assuming *buffer results in a pointer to an object that contains at least count bytes, and that fd1 and fd2 are valid file descriptors, then the code you have presented will pass the same pointer value and count to each call to write(). write() will not alter the contents in any way, and the pass by value semantics of function calls would prevent it from modifying the pointer. The only way for the pointer value to be changed would be if write() was somehow defined to be a macro on your system.

    Assuming write() is not a macro, even though each call passes the same arguments, each call may have ended up writing a different number of bytes. You have to check the return value of write() to see how many bytes of the *buffer was written.