1WRITE(2)                   Linux Programmer's Manual                  WRITE(2)
2
3
4

NAME

6       write - write to a file descriptor
7

SYNOPSIS

9       #include <unistd.h>
10
11       ssize_t write(int fd, const void *buf, size_t count);
12

DESCRIPTION

14       write()  writes  up  to  count bytes from the buffer pointed buf to the
15       file referred to by the file descriptor fd.
16
17       The number of bytes written may be less than  count  if,  for  example,
18       there  is  insufficient space on the underlying physical medium, or the
19       RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)),  or  the
20       call was interrupted by a signal handler after having written less than
21       count bytes.  (See also pipe(7).)
22
23       For a seekable file (i.e., one to which lseek(2) may  be  applied,  for
24       example,  a  regular file) writing takes place at the current file off‐
25       set, and the file offset is incremented by the number of bytes actually
26       written.   If  the file was open(2)ed with O_APPEND, the file offset is
27       first set to the end of the file before writing.  The adjustment of the
28       file offset and the write operation are performed as an atomic step.
29
30       POSIX  requires  that  a  read(2)  which can be proved to occur after a
31       write() has returned returns the new data.  Note that not all file sys‐
32       tems are POSIX conforming.
33

RETURN VALUE

35       On  success,  the  number  of bytes written is returned (zero indicates
36       nothing was written).  On error, -1  is  returned,  and  errno  is  set
37       appropriately.
38
39       If  count  is  zero  and  fd refers to a regular file, then write() may
40       return a failure status if one of the errors below is detected.  If  no
41       errors  are  detected,  0  will  be  returned without causing any other
42       effect.  If count is zero and fd refers to a file other than a  regular
43       file, the results are not specified.
44

ERRORS

46       EAGAIN The  file descriptor fd refers to a file other than a socket and
47              has been marked nonblocking (O_NONBLOCK), and  the  write  would
48              block.
49
50       EAGAIN or EWOULDBLOCK
51              The  file  descriptor  fd refers to a socket and has been marked
52              nonblocking   (O_NONBLOCK),   and   the   write   would   block.
53              POSIX.1-2001  allows  either error to be returned for this case,
54              and does not require these constants to have the same value,  so
55              a portable application should check for both possibilities.
56
57       EBADF  fd is not a valid file descriptor or is not open for writing.
58
59       EDESTADDRREQ
60              fd  refers to a datagram socket for which a peer address has not
61              been set using connect(2).
62
63       EDQUOT The user's quota of disk blocks on the  file  system  containing
64              the file referred to by fd has been exhausted.
65
66       EFAULT buf is outside your accessible address space.
67
68       EFBIG  An attempt was made to write a file that exceeds the implementa‐
69              tion-defined maximum file size or the process's file size limit,
70              or to write at a position past the maximum allowed offset.
71
72       EINTR  The  call  was interrupted by a signal before any data was writ‐
73              ten; see signal(7).
74
75       EINVAL fd is attached to an object which is unsuitable for writing;  or
76              the  file  was  opened  with  the  O_DIRECT flag, and either the
77              address specified in buf, the value specified in count,  or  the
78              current file offset is not suitably aligned.
79
80       EIO    A low-level I/O error occurred while modifying the inode.
81
82       ENOSPC The device containing the file referred to by fd has no room for
83              the data.
84
85       EPIPE  fd is connected to a pipe or socket whose reading end is closed.
86              When  this  happens the writing process will also receive a SIG‐
87              PIPE signal.  (Thus, the write return value is seen only if  the
88              program catches, blocks or ignores this signal.)
89
90       Other errors may occur, depending on the object connected to fd.
91

CONFORMING TO

93       SVr4, 4.3BSD, POSIX.1-2001.
94
95       Under  SVr4  a  write may be interrupted and return EINTR at any point,
96       not just before any data is written.
97

NOTES

99       A successful return from write() does not make any guarantee that  data
100       has been committed to disk.  In fact, on some buggy implementations, it
101       does not even guarantee that space has successfully been  reserved  for
102       the  data.   The  only way to be sure is to call fsync(2) after you are
103       done writing all your data.
104
105       If a write() is interrupted by a signal handler before  any  bytes  are
106       written, then the call fails with the error EINTR; if it is interrupted
107       after at least one byte  has  been  written,  the  call  succeeds,  and
108       returns the number of bytes written.
109

SEE ALSO

111       close(2),  fcntl(2),  fsync(2), ioctl(2), lseek(2), open(2), pwrite(2),
112       read(2), select(2), writev(2), fwrite(3)
113

COLOPHON

115       This page is part of release 3.53 of the Linux  man-pages  project.   A
116       description  of  the project, and information about reporting bugs, can
117       be found at http://www.kernel.org/doc/man-pages/.
118
119
120
121Linux                             2013-01-27                          WRITE(2)
Impressum