(standard c libraries translation )write

write - write to a file descriptor
write - 写到一个文件描述符

所需头文件
#include <unistd.h>

ssize_t write(int fd, const void *buf, size_t count);


write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.
write从指向buf的缓冲区写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的限制,或者还没写到count字节,调用被信号中断了

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.
对于一个可以查找的文件,write从当前文件的偏移量开始写,然后文件偏移量会增加写入的字节数,如果文件通过open或者ed,以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要求read能够在write返回之后返回新的数据,需要注意的是不是所有文件系统都是POSIX认证的

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,errno被设置成适当的值

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不是关联的一个常规文件,结果是未定义的

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关联到文件,而不是标记为非阻塞的socket,write被阻塞住了
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关联到非阻塞的socket,write被阻塞住了,POSIX.1-2001允许这个错误返回,而且没有要求这两个常量具有相同的值,所以可移植的程序应该这两种可能性都要检测
EBADF  fd is not a valid file descriptor or is not open for writing.
fd不是一个可用的文件描述符,或者以可读的方式打开
EDESTADDRREQ fd refers to a datagram socket for which a peer address has not been set using connect(2).
fd关联到数据包socket,对端地址没有使用connect
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.
在修改indoe的时候发生了低级的I/O错误
ENOSPC The device containing the file referred to by fd has no room for the data.
fd所关联的文件没有空间来写数据
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已经关闭了,如果发生这种问题,写进程同时会受到一个SIGPIE的信号
Other errors may occur, depending on the object connected to fd.
也可能发生其他错误,取决于fd所关联的对象


Under SVr4 a write may be interrupted and return EINTR at any point, not just before any data is written.
在SVR4下,写操作在任何阶段都可能会被中断,然后返回EINTR,而不仅仅是在写操作之前

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的成功返回并不能保证数据已经被写到了磁盘,实际上,在一些有问题的实现中,它甚至不保证数据所需要的空间被申请,只能在写完所有数据之后调用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在写任何数据之前被信号中断,调用就会发生EINTR错误,如果中断发生在写数据之后,调用会返回成功,返回写入的数据字节数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值