📄 read.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>read</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_007_1897"> </a>NAME</h4><blockquote>read, readv, pread - read from a file</blockquote><h4><a name = "tag_000_007_1898"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="unistd.h.html">unistd.h</a>>ssize_t read(int <i>fildes</i>, void *<i>buf</i>, size_t <i>nbyte</i>);ssize_t pread(int <i>fildes</i>, void *<i>buf</i>, size_t <i>nbyte</i>, off_t offset);#include <<a href="sysuio.h.html">sys/uio.h</a>>ssize_t readv(int <i>fildes</i>, const struct iovec *<i>iov</i>, int <i>iovcnt</i>);</code></pre></blockquote><h4><a name = "tag_000_007_1899"> </a>DESCRIPTION</h4><blockquote>The<i>read()</i>function attempts to read<i>nbyte</i>bytes from the file associated with the open file descriptor,<i>fildes</i>,into the buffer pointed to by<i>buf</i>.<p>If<i>nbyte</i>is 0,<i>read()</i>will return 0 and have no other results.<p>On files that support seeking (for example, a regular file), the<i>read()</i>starts at a position in the file given by the file offset associated with<i>fildes</i>.The file offset is incremented by the number of bytes actually read.<p>Files that do not support seeking,for example, terminals,always read from the currentposition. The value of a file offsetassociated with such a file is undefined.<p>No data transfer will occur past the current end-of-file. If the startingposition is at or after the end-of-file, 0 will be returned. If the filerefers to a device special file, the result of subsequent<i>read()</i>requests is implementation-dependent.<p>If the value of<i>nbyte</i>is greater than {SSIZE_MAX}, the result is implementation-dependent.<p>When attempting to read from an empty pipe or FIFO:<ul><p><li>If no process has the pipe open for writing,<i>read()</i>will return 0 to indicate end-of-file.<p><li>If some process has the pipe open for writing and O_NONBLOCK is set,<i>read()</i>will return -1 and set<i>errno</i>to [EAGAIN].<p><li>If some process has the pipe open for writing and O_NONBLOCK is clear,<i>read()</i>will blockthe calling threaduntil some data is written or thepipe is closed by all processes that had thepipe open for writing.<p></ul><p>When attempting to read a file (other than a pipe or FIFO)that supports non-blocking reads and has no data currently available:<ul><p><li>If O_NONBLOCK is set,<i>read()</i>will return a -1 and set<i>errno</i>to [EAGAIN].<p><li>If O_NONBLOCK is clear,<i>read()</i>will block the calling threaduntil some data becomes available.<p><li>The use of the O_NONBLOCKflag has no effect if there is some data available.<p></ul><p>The<i>read()</i>function reads data previously written to a file. If any portion of a regularfile prior to the end-of-file has not been written,<i>read()</i>returns bytes with value 0. For example,<i><a href="lseek.html">lseek()</a></i>allows the file offset to be set beyond the end of existing data in the file.If data is later written at this point, subsequent readsin the gap between the previous end of data and the newly written datawill return bytes with value 0 until data is written into the gap.<p>Upon successful completion, where<i>nbyte</i>is greater than 0,<i>read()</i>will mark for update the <i>st_atime</i>field of the file,and return the number of bytes read.This number will never be greater than<i>nbyte</i>.The value returned may be less than<i>nbyte</i>if the number of bytes left in the file is less than<i>nbyte</i>,if the<i>read()</i>request was interrupted by a signal, or if the fileis a pipe or FIFO or special file and has fewer than<i>nbyte</i>bytes immediately available for reading. For example, a<i>read()</i>from a file associated with a terminal may returnone typed line of data.<p>If a<i>read()</i>is interrupted by a signal before it reads any data,it will return -1 with<i>errno</i>set to [EINTR].<p>If a<i>read()</i>is interrupted by a signal after it has successfully readsome data,it will return the number of bytes read.<p>A<i>read()</i>from a STREAMS file can read data in three different modes: byte-stream mode,message-nondiscard mode, and message-discard mode. The default is byte-streammode. This can be changed using the I_SRDOPT<i><a href="ioctl.html">ioctl()</a></i>request, and can be tested with the I_GRDOPT<i><a href="ioctl.html">ioctl()</a></i>.In byte-stream mode,<i>read()</i>retrieves data from the STREAM until as many bytes as were requested aretransferred, or until there is no more data to be retrieved. Byte-stream modeignores message boundaries.<p>In STREAMS message-nondiscard mode,<i>read()</i>retrieves data until as many bytes as were requested are transferred, or untila message boundary is reached. If<i>read()</i>does not retrieve all the data in a message, the remaining data is left onthe STREAM, and can be retrieved by the next<i>read()</i>call. Message-discard mode also retrieves data until as many bytes as wererequested are transferred, or a message boundary is reached. However, unreaddata remaining in a message after the<i>read()</i>returns is discarded,and is not available for a subsequent<i>read()</i>,<i><a href="readv.html">readv()</a></i>or<i><a href="getmsg.html">getmsg()</a></i>call.<p>How<i>read()</i>handles zero-byte STREAMS messages is determined by the current read modesetting. In byte-stream mode,<i>read()</i>accepts data until it has read<i>nbyte</i> bytes, or until there is no more datato read, or until a zero-byte message block is encountered.The<i>read()</i>functionthen returns the number of bytes read, and places the zero-bytemessage back on the STREAM to be retrieved by thenext<i>read()</i>,<i><a href="readv.html">readv()</a></i>or<i><a href="getmsg.html">getmsg()</a></i>.In message-nondiscard mode or message-discard mode, a zero-byte messagereturns 0 and the message is removed from the STREAM. When azero-byte message is read as the first message on a STREAM, the message isremoved from the STREAM and 0 is returned, regardless of the read mode.<p>A<i>read()</i>from a STREAMS file returns the data in the message at the front of the STREAMhead read queue, regardless of the priority band of the message.<p>By default, STREAMs are in control-normal mode, in which a<i>read()</i>from a STREAMS file can only process messages that contain a data part but donot contain a control part. The<i>read()</i>fails if a message containing a control part is encountered at the STREAMhead. This default action can be changed by placing the STREAM in eithercontrol-data mode or control-discard mode with the I_SRDOPT<i><a href="ioctl.html">ioctl()</a></i>command. In control-data mode,<i>read()</i>converts any control part to data and passes it to the application beforepassing any data part originally present in the same message. Incontrol-discard mode,<i>read()</i>discards message control parts but returns to the process any data part in themessage.<p>In addition,<i>read()</i>and<i><a href="readv.html">readv()</a></i>will fail if the STREAM head had processed an asynchronous error before thecall. In this case, the value of<i>errno</i>does not reflect the result of<i>read()</i>or<i><a href="readv.html">readv()</a></i>but reflects the prior error.If a hangup occurs on the STREAM being read,<i>read()</i>continues to operate normally until the STREAM head read queue is empty.Thereafter, it returns 0.<p>The<i><a href="readv.html">readv()</a></i>function is equivalent to<i>read()</i>,but places the input data into the<i>iovcnt</i>buffers specified by the members of the<i>iov</i>array: <i>iov</i>[0], <i>iov</i>[1], ...,<i>iov</i>[<i>iovcnt</i>-1].The <i>iovcnt</i> argument is valid if greater than 0 and less than or equal to{IOV_MAX}.<p>Each <i>iovec</i> entry specifies the base address and length of an area inmemory where data should be placed. The<i><a href="readv.html">readv()</a></i>function always fills an area completely before proceeding to the next.<p>Upon successful completion,<i><a href="readv.html">readv()</a></i>marks for update the <i>st_atime</i> field of the file.<p>If the Synchronized Input and Output option is supported:<dl compact><dt> <dd>If the O_DSYNC and O_RSYNC bits have been set,read I/O operations on the file descriptor complete as defined bysynchronised I/O data integrity completion.If the O_SYNC and O_RSYNC bits have been set,read I/O operations on the file descriptor complete as defined bysynchronised I/O file integrity completion.</dl><p>If the Shared Memory Objects option is supported:<dl compact><dt> <dd>If<i>fildes</i>refers to a shared memory object, the result of the<i>read()</i>function is unspecified.</dl><p>For regular files, no data transfer will occur past the offset maximumestablished in the open file description associated with<i>fildes</i>.<p>The<i><a href="pread.html">pread()</a></i>function performs the same action as<i>read()</i>,except that it reads from a given position in the file withoutchanging the file pointer.The first three arguments to<i><a href="pread.html">pread()</a></i>are the same as<i>read()</i>with the addition of a fourth argument offset for the desired positioninside the file.An attempt to perform a<i><a href="pread.html">pread()</a></i>on a file that is incapable of seeking results in an error.</blockquote><h4><a name = "tag_000_007_1900"> </a>RETURN VALUE</h4><blockquote>Upon successful completion,<i>read()</i>,<i><a href="pread.html">pread()</a></i>and<i><a href="readv.html">readv()</a></i>return a non-negative integer indicating the number of bytes actually read.Otherwise, the functions return -1 and set <i>errno</i> to indicate theerror.</blockquote><h4><a name = "tag_000_007_1901"> </a>ERRORS</h4><blockquote>The<i>read()</i>,<i><a href="pread.html">pread()</a></i>and<i><a href="readv.html">readv()</a></i>functions will fail if:<dl compact><dt>[EAGAIN]<dd>The O_NONBLOCK flag is set for the file descriptor and the processwould be delayed.<dt>[EBADF]<dd>The<i>fildes</i>argument is not a valid file descriptor open for reading.<dt>[EBADMSG]<dd>The file is a STREAM file that is set to control-normal modeand the message waiting to be read includes a control part.<dt>[EINTR]<dd>The read operation was terminated due to the receiptof a signal, and no data was transferred.<dt>[EINVAL]<dd>The STREAM or multiplexer referenced by <i>fildes</i> is linked (directly orindirectly) downstream from a multiplexer.<dt>[EIO]<dd>A physical I/O error has occurred.<dt>[EIO]<dd>The process is a memberof a background process attempting to read from its controllingterminal, the process is ignoring or blocking the SIGTTIN signalor the process group is orphaned. This error may also be generated forimplementation-dependent reasons.<dt>[EISDIR]<dd>The <i>fildes</i> argument refers to a directory and the implementationdoes not allow the directory to be read using<i>read()</i>,<i><a href="pread.html">pread()</a></i>or<i><a href="readv.html">readv()</a></i>.The<i><a href="readdir.html">readdir()</a></i>function should be used instead.<dt>[EOVERFLOW]<dd>The file is a regular file,<i>nbyte</i>is greater than 0, the startingposition is before the end-of-file and the starting position isgreater than or equal to the offset maximum established in the openfile description associated with<i>fildes</i>.</dl><p>The<i><a href="readv.html">readv()</a></i>function will fail if:<dl compact><dt>[EINVAL]<dd>The sum of the <i>iov_len</i> values in the <i>iov</i> array overflowed an<b>ssize_t</b>.</dl><p>The<i>read()</i>,<i><a href="pread.html">pread()</a></i>and<i><a href="readv.html">readv()</a></i>functions may fail if:<dl compact><dt>[ENXIO]<dd>A request was made of a non-existent device, or the request wasoutside the capabilities of the device.</dl><p>The<i><a href="readv.html">readv()</a></i>function may fail if:<dl compact><dt>[EINVAL]<dd>The <i>iovcnt</i> argument was less than or equal to 0, or greater than{IOV_MAX}.</dl><p>The<i><a href="pread.html">pread()</a></i>function will fail, and the file pointer remains unchanged, if:<dl compact><dt>[EINVAL]<dd>The<i>offset</i>argument is invalid.The value is negative.<dt>[EOVERFLOW]<dd>The file is a regular file and an attempt was made to read or write ator beyond the offset maximum associated with the file.<dt>[ENXIO]<dd>A request was outside the capabilities of the device.<dt>[ESPIPE]<dd><i>fildes</i> is associated with a pipe or FIFO.</dl></blockquote><h4><a name = "tag_000_007_1902"> </a>EXAMPLES</h4><blockquote>None.</blockquote><h4><a name = "tag_000_007_1903"> </a>APPLICATION USAGE</h4><blockquote>None.</blockquote><h4><a name = "tag_000_007_1904"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_007_1905"> </a>SEE ALSO</h4><blockquote><i><a href="fcntl.html">fcntl()</a></i>,<i><a href="ioctl.html">ioctl()</a></i>,<i><a href="lseek.html">lseek()</a></i>,<i><a href="open.html">open()</a></i>,<i><a href="pipe.html">pipe()</a></i>,<i><a href="stropts.h.html"><stropts.h></a></i>,<i><a href="sysuio.h.html"><sys/uio.h></a></i>,<i><a href="unistd.h.html"><unistd.h></a></i>,<b>XBD</b> specification, <a href="../xbd/termios.html"><b>General Terminal Interface</b> </a>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from Issue 1 of the SVID.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -