C Programming/POSIX Reference/unistd.h/write

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The write system call writes data, in bytes as specified by the caller, from a buffer declared by the user in the program and then writes it into the file supplied by the calling process. In most modern operating system, a program that needs to write data to a file stored in a filesystem uses the Write system call. The file is identified by the file descriptor that is obtained from a previous call to open.

Write, thus, takes three arguments:

  1. The file descriptor of the file (fd).
  2. The buffer from where data is to be written into the file (buf).
  3. The number of bytes to be read from the buffer (nbytes).

POSIX usage[edit | edit source]

The write system 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
fd
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 .
buf
It points to a character array, which can be used to store content obtained from the file pointed to by fd.
nbytes
It specifies the number of bytes to be written from the file into the character array.

In above syntax, ssize_t is a typedef. It is a signed data type defined in sys/types.h. sizeof operator produces an integer value which is of type size_t.[4]
The write function returns the number of bytes successfully written into the array, which may at times be less than the specified nbytes. It returns -1 if an error is encountered.

Usage Example[edit | edit source]

#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 | O_CREAT);
    if (fd1 == -1) {
        perror("File cannot be opened");
        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 */
 
    close(fd1);
    return 0;
}

Errors encountered during operation[edit | edit source]

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

Error Numbers Error Meaning
5
EIO
Low-level errors, often concerned with hardware read/write operations.
9
EBADF
The file descriptor fd is not valid, or an attempt is being made to write into a file opened in 'read-only' mode.
14
EFAULT
The address specified in the function is an invalid address.
22
EINVAL
The argument(s) passed with the function is(are) invalid.
28
ENOSPC
No space available for writing onto the disc.

References[edit | edit source]

  1. http://www.unix.com/man-page/FreeBSD/2/write/ Manual page for Write
  2. http://www.gnu.org/s/hello/manual/libc/I_002fO-Primitives.html#I_002fO-Primitives I/O Primitives
  3. http://pubs.opengroup.org/onlinepubs/007904875/functions/write.html
  4. The C programming language. Brian Kernighan & Dennis Ritchie. India: PHI Learning Private Limited. ISBN 978-81-203-0596-0.
  5. http://www.gnu.org/s/hello/manual/libc/Error-Codes.html GNU C Library Manual
  6. http://www.ibm.com/developerworks/aix/library/au-errnovariable/ IBM page listing errors

External links[edit | edit source]