write (system call)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

The write is one of the most basic routines provided by a Unix-like operating system kernel. It writes data from a buffer declared by the user to a given device, such as a file. This is the primary way to output data from a program by directly using a system call. The destination is identified by a numeric code. The data to be written, for instance a piece of text, is defined by a pointer and a size, given in number of bytes.

write thus takes three arguments:

  1. The file code (file descriptor or fd).
  2. The pointer to a buffer where the data is stored (buf).
  3. The number of bytes to write from the buffer (nbytes).

POSIX usage[edit]

The write call interface[1][2][3] is standardized by the POSIX specification. Data is written to a file by calling the write function. The function prototype is:

 ssize_t write(int fd, const void *buf, size_t nbytes);
Argument Description
It is the file descriptor which has been obtained from the call to open. It is an integer value. The values 0, 1, 2 can also be given, for standard input, standard output & standard error, respectively .
It points to a character array, with content to be written to the file pointed to by fd.
It specifies the number of bytes to be written from the character array into the file pointed to by fd.

In above syntax, ssize_t is a typedef. It is a signed data type defined in stddef.h. Note that write() does not return an unsigned value; it returns -1 if an error occurs so it must return a signed value.
The write function returns the number of bytes successfully written into the file, which may at times be less than the specified nbytes. It returns -1 if an exceptional condition is encountered, see section on errors below.

Usage example[edit]

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main (int argc, char *argv[])
    int fd1;
    char buf[128];
    fd1 = open(argv[1], O_WRONLY);
    if (fd1 == -1) {
        return EXIT_FAILURE;

    /* Enter the data to be written into the file */
    scanf("%127s", buf);

    write(fd1, buf, strlen(buf)); /* fd1 is the file descriptor, buf is the character array used to
 hold the data, strlen(buf) informs the function that the number of bytes equal to the length of the
 string in the buffer need to be copied */


    return 0;

Errors encountered during operation[edit]

Listed below are some errors[4][5] that could be encountered during writing to a file. The errors are macros listed in errno.h.

Error Numbers Error Meaning
The system call was interrupted.
Low-level errors, often concerned with hardware read/write operations.
The file descriptor fd is not valid, or an attempt is being made to write into a file opened in 'read-only' mode.
The user does not have the necessary permissions to write into the file.
The address specified in the function is an invalid address.
The argument(s) passed with the function is(are) invalid.
The file size specified in nbytes is too large, and is greater than that allowed by the system.
No space available for writing onto the storage device.
The pipe is either broken, or the file at the other end of the pipe is not open for I/O purposes (most processes giving this type of error also generate the SIGPIPE signal).

Higher level I/O functions calling write[edit]

High-level I/O functions calling write

The write system call is not an ordinary function, in spite of the close resemblance. For example, in Linux with the x86 architecture, the system call uses the instruction INT 80H, in order to transfer control over to the kernel.[6] The write system call, and its counterpart read, being low level functions, are only capable of understanding bytes. Write cannot be used to write records, like classes. Thus, higher level input-output functions (like printf) are required. Often, the high-level interface is preferred, as compared to the cluttered low-level interface. These functions call other functions internally, and these in turn can make calls to write, giving rise to a layered assembly of functions.[7][8]

With the use of this assembly the higher level functions can collect bytes of data and then write the required data into a file.

See also[edit]


  1. ^ http://www.unix.com/man-page/FreeBSD/2/write/ Manual page for Write
  2. ^ https://www.gnu.org/s/hello/manual/libc/I_002fO-Primitives.html#I_002fO-Primitives I/O Primitives
  3. ^ "Write".
  4. ^ https://www.gnu.org/s/hello/manual/libc/Error-Codes.html GNU C Library Manual
  5. ^ http://www.ibm.com/developerworks/aix/library/au-errnovariable/ IBM page listing errors
  6. ^ LINUX assembly language programming By Bob Neveln ISBN 978-0-13-087940-0
  7. ^ https://www.gnu.org/s/libc/ GNU C Library
  8. ^ http://ftp.gnu.org/gnu/glibc/ GNU C Library Download

External links[edit]