
The POSIX standard defines the write()
interface as follows [1].
ssize_t write (int filedes, const void *buffer, size_t size);
The write function writes up to size bytes from buffer to the file with descriptor filedes. The data in buffer is not necessarily a character string and a null character is output like any other character.
The definition does not state that the write()
function will stop copying characters into the file if a null character is encountered. Therefore, when writing a C string in to a file using the write()
function, always use the size of the string plus 1 (for the null character) as the size
parameter.
Noncompliant Code Example
Compliant Code Example
References
[1] http://www.gnu.org/software/libc/manual/html_node/I_002fO-Primitives.html#I_002fO-Primitives