write

Writes data (blocks even if writing to the screen).

Library:LibC
Classification:POSIX
Service:File and Directory I/O

Syntax

  #include <unistd.h> 
   
  ssize_t write (
     int           fildes,
     const void   *buf,
     size_t        nbytes);
  

Parameters

fildes

(IN) Specifies a file descriptor.

buf

(IN) Points to the address at which to start transmitting data.

nbytes

(IN) Specifies the number of bytes to write.

Return Values

If successful, returns the number of bytes of data actually written. Otherwise, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

4

EBADF

Bad file number.

9

EINVAL

Invalid file descriptor.

12

ENOSPC

No free space remains on the device containing the file.

14

ERANGE

The transfer request size was outside the range supported by the STREAMS file associated with fildes.

24

EAGAIN

The file was flagged for non-blocking I/O, and no data could be written immediately.

25

ENXIO

Either a hang up occurred or the device does not exist.

28

EIO

An I/O error occurred while reading from or writing to the file system.

32

EPIPE

The pipe or FIFO has not been opened for reading or cannot be written to.

54

ECONNRESET

A write was attempted on a socket that is not connected.

55

ENOBUFS

Insufficient resources.

63

EINTR

The write operation was interrupted by a signal.

71

EFBIG

An attempt was made to write a file that exceeds the file size limit or the maximum file size.

Remarks

The write function attempts to write nbytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes.

The fildes value is returned by open, sopen, or creat. The access mode must have included either O_WRONLY or O_RDWR when the file was opened.

The data is written to the file at the end when the file was opened with O_APPEND included as part of the access mode; otherwise, it is written at the current file position for the file in question. This file position can be determined with tell and can be set with lseek.

See Also