www.delorie.com/djgpp/doc/libc/libc_866.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]



#include <unistd.h>

int write(int file, const void *buffer, size_t count);


This function writes count bytes from buffer to file. It returns the number of bytes actually written. It will return zero or a number less than count if the disk is full, and may return less than count even under valid conditions.

Note that if file is a text file, write may write more bytes than it reports.

If count is zero, the function does nothing and returns zero. Use _write if you want to actually ask DOS to write zero bytes.

The precise behavior of write when the target filesystem is full is somewhat troublesome, because DOS doesn't fail the underlying system call. If your application needs to rely on errno being set to ENOSPC in such cases, you need to invoke write as shown in the example below. In a nutshell, the trick is to call write one more time after it returns a value smaller than the count parameter; then it will always set errno if the disk is full.

Return Value

The number of bytes written, zero at EOF, or -1 on error.


POSIX 1003.2-1992; 1003.1-2001


This example shows how to call write in a way which ensures that errno will be set to ENOSPC if the target filesystem is or becomes full:

  char *buf_ptr;    /* the buffer to write */
  size_t buf_len;   /* the number of bytes to write */
  int desc;         /* the file descriptor to write to */

  while (buf_len > 0)
    int written = write (desc, buf_ptr, buf_len);
    if (written <= 0)

    buf_ptr += written;
    buf_len -= written;

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2004   by DJ Delorie     Updated Apr 2004