NAME
write -在一个文件描述符上执行写操作
概述
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
描述
write 向文件描述符 fd 所引用的文件中写入从 buf 开始的缓冲区中 count 字节的数据. POSIX规定,当使用了write()之后再使用 read(),那么读取到的应该是更新后的数据. 但请注意并不是所有的文件系统都是 POSIX兼容的.
返回值
成功时返回所写入的字节数(若为零则表示没有写入数据). 错误时返回-1,并置errno为相应值. 若count为零,对于普通文件无任何影响,但对特殊文件将产生不可预料的后果.
错误代码
- EBADF
- fd 不是一个合法的文件描述符或者没有以写方式打开.
- EINVAL
- fd 所指向的对象不可写.
- EFAULT
- buf 不在用户可访问地址空间内.
- EPIPE
- fd 连接到一个管道,或者套接字的读方向一端已关闭.此时写进程将接收到 SIGPIPE 信号;如果此信号被捕获,阻塞或忽略,那么将返回错误 EPIPE.
- EAGAIN
- 读操作阻塞,但使用 O_NONBLOCK 指定了非阻塞式输入输出.
- EINTR
- 在写数据以前调用被信号中断.
- ENOSPC
- fd 指向的文件所在的设备无可用空间.
- EIO
- 当编辑一个节点时发生了底层输入输出错误.
可能发生了其他错误,取决于 fd 所连接的对象.
兼容于
SVr4, SVID, POSIX, X/OPEN, 4.3BSD. SVr4文档添加了以下错误代码: EDEADLK, EFBIG, ENOLCK, ENOLNK, ENOSR, ENXIO, EPIPE,或者ERANGE. 对于SVr4有可能在写入部分数据时发生中断并返回EINTR.
参见
open(2), read(2), fcntl(2), close(2), lseek(2), select(2), ioctl(2), fsync(2), fwrite(3)
#p#
NAME
write - write to a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
DESCRIPTION
write writes up to count bytes to the file referenced by the file descriptor fd from the buffer starting at buf. POSIX requires that a read() which can be proved to occur after a write() has returned returns the new data. Note that not all file systems are POSIX conforming.
RETURN VALUE
On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and errno is set appropriately. If count is zero and the file descriptor refers to a regular file, 0 will be returned without causing any other effect. For a special file, the results are not portable.
ERRORS
- EBADF
- fd is not a valid file descriptor or is not open for writing.
- EINVAL
- fd is attached to an object which is unsuitable for writing.
- EFAULT
- buf is outside your accessible address space.
- EFBIG
- An attempt was made to write a file that exceeds the implementation-defined maximum file size or the process' file size limit, or to write at a position past than the maximum allowed offset.
- 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.)
- EAGAIN
- Non-blocking I/O has been selected using O_NONBLOCK and the write would block.
- EINTR
- The call was interrupted by a signal before any data was written.
- ENOSPC
- The device containing the file referred to by fd has no room for the data.
- EIO
- A low-level I/O error occurred while modifying the inode.
Other errors may occur, depending on the object connected to fd.
CONFORMING TO
SVr4, SVID, POSIX, X/OPEN, 4.3BSD. SVr4 documents additional error conditions EDEADLK, ENOLCK, ENOLNK, ENOSR, ENXIO, or ERANGE. Under SVr4 a write may be interrupted and return EINTR at any point, not just before any data is written.
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.
SEE ALSO
close(2), fcntl(2), fsync(2), ioctl(2), lseek(2), open(2), read(2), select(2), fwrite(3), writev(3)