WRITE(2) Linux Programmer's Manual WRITE(2)
NAME
write - write to a file descriptor
//向fd中写入
SYNOPSIS
#include <unistd.h> ssize_t write(int fd, const void *buf, size_t count);
DESCRIPTION
write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd. //write()向由fd指定的文件的缓冲中全部写出count个字节 The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).) //写入的字节可能会比要求的count要少,比如,在物理介质上没有空间,或者是遇到RLIMIT_FSIZE资源限制
//(参见setrlimit()).或者是调用被一个信号中断. For a seekable file (i.e., one to which lseek(2) may be applied, for example, a regular file) writing takes place at the current file offset, and the file offset is incremented by the number of bytes actually written. If the file was open(2)ed with O_APPEND, the file offset is first set to the end of the file before writing. The adjustment of the file offset and the write operation are performed as an atomic step. //对于一个可seek的文件(使用lseek()可能出现,例如,一个规律的文件)在当前文件偏移的位置替换写,
//偏移的值在随着实际的写入而增加着.如果文件使用O_APPEND打开,在写之前文件的偏移设置为文件的末尾.
//文件的偏移和写操作的调解是以原子操作进行的. POSIX requires that a read(2) which can be proved to occur after a write() has returned returns the new data. Note that not all file systems are POSIX conforming.
//POSIX需要读操作返回的数据证明是在写操作之后的发生的.注意,不是所有的文件系统都遵循POSIX标准.
RETURN VALUE
On success, the number of bytes written is returned (zero indicates nothing
was written). On error, -1 is returned, and errno is set appropriately.
//成功:返回写入的字节数(0标识没有任何数据写入).
//失败:-1
If count is zero and fd refers to a regular file, then write() may return a
failure status if one of the errors below is detected. If no errors are
detected, 0 will be returned without causing any other effect. If count is
zero and fd refers to a file other than a regular file, the results are not
specified.
//如果count是0而fd指向一个正常的文件,那么write()可能返回失败的情形是:如果是下面的错误被检测到的话.
//如果没有错误检测到,返回0,但是没有其他的任何作用.
//如果count是0而fd指向不正常的文件,结果是没有定义的.
ERRORS
EAGAIN The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the write would block. //fd指向的是一个file而不是一个标记了O_NONBLOCK的socket,写操作会被阻塞. EAGAIN or EWOULDBLOCK The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the write would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities. //fd指向的是一个标记了O_NONBLOCK的socket,写操作会被阻塞.POSIX.1-2001允许在这种情况下返回任一种错误,
//不需要这些常量具有相同的值.所以一个轻便的程序应该检验这2种情况. EBADF fd is not a valid file descriptor or is not open for writing. //fd无效或者是fd还没有被打开 EDESTADDRREQ fd refers to a datagram socket for which a peer address has not been set using connect(2). //如果fd是一个数据报socket,并且地址并没有使用connect进行连接的话.----新增加的返回值,2009-2-23无此项 EFAULT buf is outside your accessible address space. //buf在你可以访问的地址之外 EFBIG An attempt was made to write a file that exceeds the implementation- defined maximum file size or the process's file size limit, or to write at a position past the maximum allowed offset. //尝试向文件中写入的数据超过了文件的最大值的限制或者是进程文件的大小限制,或者是准备写入的位置
//超过了允许的最大偏移值. EINTR The call was interrupted by a signal before any data was written; see signal(7). //在写之前被信号中断 EINVAL fd is attached to an object which is unsuitable for writing; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned. //fd尝试向并不适合写的对象中写入数据.或者是文件使用了O_DIRECT打开,或者是在buf中没有指定地址,及
//没有指定count,或者是当前文件的偏移值成一线不合适? EIO A low-level I/O error occurred while modifying the inode. //当改变结点的适合发生了低级的I/O发生 ENOSPC The device containing the file referred to by fd has no room for the data. //磁盘没有空间 EPIPE fd is connected to a pipe or socket whose reading end is closed. When this happens the writing process will also receive a SIGPIPE signal. (Thus, the write return value is seen only if the program catches, blocks or ignores this signal.) //fd连接的是一个pipe或者是需要读的socket已经关闭.当这种情况发生的时候,写进程将会收到一个SIGPIPE信号,
//(这样,write的返回值只有当如果进程遇到,阻塞或者是忽略这个信号时才可见) Other errors may occur, depending on the object connected to fd.
//其他错误也可能发生,这取决于fd连接的对象
CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001. Under SVr4 a write may be interrupted and return EINTR at any point, not just before any data is written.
//在SVr4下,write会在任何时候被中断并返回一个EUNTR,不仅仅是在数据写之前.
NOTES
A successful return from write() does not make any guarantee that data has been committed to disk. In fact, on some buggy implementations, it does not even guarantee that space has successfully been reserved for the data. The only way to be sure is to call fsync(2) after you are done writing all your data. //write()的成功返回并不完全保证数据都写在了硬盘上,事实上,当某些bug发生的时候,不能保证
//为写的数据保留出了足够的空间.唯一能够确认的就是调用fsync()在你写完你的数据之后. If a write() is interrupted by a signal handler before any bytes are written, then the call fails with the error EINTR; if it is interrupted after at least one byte has been written, the call succeeds, and returns the number of bytes written.
//如果write()在写之前被信号中断的话,那么对write()的调用就失败,error是EINTR;
//如果write()在写了哪怕一个字节之后被信号中断的话,那么对write()的调用成功,返回的是写的实际字节
SEE ALSO
close(2), fcntl(2), fsync(2), ioctl(2), lseek(2), open(2), pwrite(2), read(2), select(2), writev(2), fwrite(3)
COLOPHON
This page is part of release 3.29 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2010-08-29 WRITE(2)