1WRITE(2) Linux Programmer's Manual WRITE(2)
2
3
4
6 write - write to a file descriptor
7
9 #include <unistd.h>
10
11 ssize_t write(int fd, const void *buf, size_t count);
12
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
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
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 EFAULT buf is outside your accessible address space.
60
61 EFBIG An attempt was made to write a file that exceeds the implementa‐
62 tion-defined maximum file size or the process's file size limit,
63 or to write at a position past the maximum allowed offset.
64
65 EINTR The call was interrupted by a signal before any data was writ‐
66 ten; see signal(7).
67
68 EINVAL fd is attached to an object which is unsuitable for writing; or
69 the file was opened with the O_DIRECT flag, and either the
70 address specified in buf, the value specified in count, or the
71 current file offset is not suitably aligned.
72
73 EIO A low-level I/O error occurred while modifying the inode.
74
75 ENOSPC The device containing the file referred to by fd has no room for
76 the data.
77
78 EPIPE fd is connected to a pipe or socket whose reading end is closed.
79 When this happens the writing process will also receive a SIG‐
80 PIPE signal. (Thus, the write return value is seen only if the
81 program catches, blocks or ignores this signal.)
82
83 Other errors may occur, depending on the object connected to fd.
84
86 SVr4, 4.3BSD, POSIX.1-2001.
87
88 Under SVr4 a write may be interrupted and return EINTR at any point,
89 not just before any data is written.
90
92 A successful return from write() does not make any guarantee that data
93 has been committed to disk. In fact, on some buggy implementations, it
94 does not even guarantee that space has successfully been reserved for
95 the data. The only way to be sure is to call fsync(2) after you are
96 done writing all your data.
97
98 If a write() is interrupted by a signal handler before any bytes are
99 written, then the call fails with the error EINTR; if it is interrupted
100 after at least one byte has been written, the call succeeds, and
101 returns the number of bytes written.
102
104 close(2), fcntl(2), fsync(2), ioctl(2), lseek(2), open(2), pwrite(2),
105 read(2), select(2), writev(2), fwrite(3)
106
108 This page is part of release 3.25 of the Linux man-pages project. A
109 description of the project, and information about reporting bugs, can
110 be found at http://www.kernel.org/doc/man-pages/.
111
112
113
114Linux 2009-02-23 WRITE(2)