⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pread.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>read</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="read"></a> <a name="tag_03_594"></a><!-- read --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright &copy; 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_594_01"></a>NAME</h4><blockquote>pread, read - read from a file</blockquote><h4><a name="tag_03_594_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/unistd.h.html">unistd.h</a>&gt;<br><br></tt></code></p><div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> ssize_t pread(int</tt> <i>fildes</i><tt>, void *</tt><i>buf</i><tt>, size_t</tt> <i>nbyte</i><tt>,off_t</tt> <i>offset</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br> ssize_t read(int</tt> <i>fildes</i><tt>, void *</tt><i>buf</i><tt>, size_t</tt> <i>nbyte</i><tt>);<br></tt></blockquote><h4><a name="tag_03_594_03"></a>DESCRIPTION</h4><blockquote><p>The <i>read</i>() function shall attempt 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>. The behavior of multiple concurrent reads on the same pipe, FIFO, orterminal device is unspecified.</p><p>Before any action described below is taken, and if <i>nbyte</i> is zero, the <i>read</i>() function may detect and return errorsas described below. In the absence of errors, or if error detection is not performed, the <i>read</i>() function shall return zeroand have no other results.</p><p>On files that support seeking (for example, a regular file), the <i>read</i>() shall start at a position in the file given bythe file offset associated with <i>fildes</i>. The file offset shall be incremented by the number of bytes actually read.</p><p>Files that do not support seeking-for example, terminals-always read from the current position. The value of a file offsetassociated with such a file is undefined.</p><p>No data transfer shall occur past the current end-of-file. If the starting position is at or after the end-of-file, 0 shall bereturned. If the file refers to a device special file, the result of subsequent <i>read</i>() requests isimplementation-defined.</p><p>If the value of <i>nbyte</i> is greater than {SSIZE_MAX}, the result is implementation-defined.</p><p>When attempting to read from an empty pipe or FIFO:</p><ul><li><p>If no process has the pipe open for writing, <i>read</i>() shall return 0 to indicate end-of-file.</p></li><li><p>If some process has the pipe open for writing and O_NONBLOCK is set, <i>read</i>() shall return -1 and set <i>errno</i> to[EAGAIN].</p></li><li><p>If some process has the pipe open for writing and O_NONBLOCK is clear, <i>read</i>() shall block the calling thread until somedata is written or the pipe is closed by all processes that had the pipe open for writing.</p></li></ul><p>When attempting to read a file (other than a pipe or FIFO) that supports non-blocking reads and has no data currentlyavailable:</p><ul><li><p>If O_NONBLOCK is set, <i>read</i>() shall return -1 and set <i>errno</i> to [EAGAIN].</p></li><li><p>If O_NONBLOCK is clear, <i>read</i>() shall block the calling thread until some data becomes available.</p></li><li><p>The use of the O_NONBLOCK flag has no effect if there is some data available.</p></li></ul><p>The <i>read</i>() function reads data previously written to a file. If any portion of a regular file prior to the end-of-filehas not been written, <i>read</i>() shall return bytes with value 0. For example, <a href="../functions/lseek.html"><i>lseek</i>()</a> allows the file offset to be set beyond the end of existing data in the file. If datais later written at this point, subsequent reads in the gap between the previous end of data and the newly written data shallreturn bytes with value 0 until data is written into the gap.</p><p>Upon successful completion, where <i>nbyte</i> is greater than 0, <i>read</i>() shall mark for update the <i>st_atime</i> fieldof the file, and shall return the number of bytes read. This number shall never be greater than <i>nbyte</i>. The value returnedmay 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 wasinterrupted by a signal, or if the file is a pipe or FIFO or special file and has fewer than <i>nbyte</i> bytes immediatelyavailable for reading. For example, a <i>read</i>() from a file associated with a terminal may return one typed line of data.</p><p>If a <i>read</i>() is interrupted by a signal before it reads any data, it shall return -1 with <i>errno</i> set to [EINTR].</p><p>If a <i>read</i>() is interrupted by a signal after it has successfully read some data, it shall return the number of bytesread.</p><p>For regular files, no data transfer shall occur past the offset maximum established in the open file description associated with<i>fildes</i>.</p><p>If <i>fildes</i> refers to a socket, <i>read</i>() shall be equivalent to <a href="../functions/recv.html"><i>recv</i>()</a>with no flags set.</p><p><sup>[<a href="javascript:open_code('SIO')">SIO</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If the O_DSYNC and O_RSYNC bits have been set, read I/O operations on the file descriptor shall complete as defined by synchronizedI/O data integrity completion. If the O_SYNC and O_RSYNC bits have been set, read I/O operations on the file descriptor shallcomplete as defined by synchronized I/O file integrity completion. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('SHM')">SHM</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If <i>fildes</i> refers to a shared memory object, the result of the <i>read</i>() function is unspecified. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('TYM')">TYM</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If <i>fildes</i> refers to a typed memory object, the result of the <i>read</i>() function is unspecified. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('XSR')">XSR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> A<i>read</i>() from a STREAMS file can read data in three different modes: <i>byte-stream</i> mode, <i>message-nondiscard</i> mode,and <i>message-discard</i> mode. The default shall be byte-stream mode. This can be changed using the I_SRDOPT <a href="../functions/ioctl.html"><i>ioctl</i>()</a> request, and can be tested with I_GRDOPT <a href="../functions/ioctl.html"><i>ioctl</i>()</a>. In byte-stream mode, <i>read</i>() shall retrieve data from the STREAM until as manybytes as were requested are transferred, or until there is no more data to be retrieved. Byte-stream mode ignores messageboundaries.</p><p>In STREAMS message-nondiscard mode, <i>read</i>() shall retrieve data until as many bytes as were requested are transferred, oruntil a message boundary is reached. If <i>read</i>() does not retrieve all the data in a message, the remaining data shall be lefton the STREAM, and can be retrieved by the next <i>read</i>() call. Message-discard mode also retrieves data until as many bytes aswere requested are transferred, or a message boundary is reached. However, unread data remaining in a message after the<i>read</i>() returns shall be discarded, and shall not be available for a subsequent <i>read</i>(), <a href="../functions/getmsg.html"><i>getmsg</i>()</a>, or <a href="../functions/getpmsg.html"><i>getpmsg</i>()</a> call.</p><p>How <i>read</i>() handles zero-byte STREAMS messages is determined by the current read mode setting. In byte-stream mode,<i>read</i>() shall accept data until it has read <i>nbyte</i> bytes, or until there is no more data to read, or until a zero-bytemessage block is encountered. The <i>read</i>() function shall then return the number of bytes read, and place the zero-bytemessage back on the STREAM to be retrieved by the next <i>read</i>(), <a href="../functions/getmsg.html"><i>getmsg</i>()</a>, or <ahref="../functions/getpmsg.html"><i>getpmsg</i>()</a>. In message-nondiscard mode or message-discard mode, a zero-byte messageshall return 0 and the message shall be removed from the STREAM. When a zero-byte message is read as the first message on a STREAM,the message shall be removed from the STREAM and 0 shall be returned, regardless of the read mode.</p><p>A <i>read</i>() from a STREAMS file shall return the data in the message at the front of the STREAM head read queue, regardlessof the priority band of the message.</p><p>By default, STREAMs are in control-normal mode, in which a <i>read</i>() from a STREAMS file can only process messages thatcontain a data part but do not contain a control part. The <i>read</i>() shall fail if a message containing a control part isencountered at the STREAM head. This default action can be changed by placing the STREAM in either control-data mode orcontrol-discard mode with the I_SRDOPT <a href="../functions/ioctl.html"><i>ioctl</i>()</a> command. In control-data mode,<i>read</i>() shall convert any control part to data and pass it to the application before passing any data part originally presentin the same message. In control-discard mode, <i>read</i>() shall discard message control parts but return to the process any datapart in the message.</p><p>In addition, <i>read</i>() shall fail if the STREAM head had processed an asynchronous error before the call. In this case, thevalue of <i>errno</i> shall not reflect the result of <i>read</i>(), but reflect the prior error. If a hangup occurs on the STREAMbeing read, <i>read</i>() shall continue to operate normally until the STREAM head read queue is empty. Thereafter, it shall return0. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The <i>pread</i>() function shall be equivalent to <i>read</i>(), except that it shall read from a given position in the filewithout changing the file pointer. The first three arguments to <i>pread</i>() are the same as <i>read</i>() with the addition of afourth argument <i>offset</i> for the desired position inside the file. An attempt to perform a <i>pread</i>() on a file that isincapable of seeking shall result in an error. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_594_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>read</i>() <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;and <i>pread</i>() <img src="../images/opt-end.gif" alt="[Option End]" border="0"> shall return a non-negative integer indicating the number of bytes actually read. Otherwise, thefunctions shall return -1 and set <i>errno</i> to indicate the error.</p></blockquote><h4><a name="tag_03_594_05"></a>ERRORS</h4><blockquote><p>The <i>read</i>() and <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> <i>pread</i>() <img src="../images/opt-end.gif" alt="[Option End]" border="0"> functions shall failif:</p><dl compact><dt>[EAGAIN]</dt><dd>The O_NONBLOCK flag is set for the file descriptor and the process would be delayed.</dd><dt>[EBADF]</dt><dd>The <i>fildes</i> argument is not a valid file descriptor open for reading.</dd><dt>[EBADMSG]</dt><dd><sup>[<a href="javascript:open_code('XSR')">XSR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The file is a STREAM file that is set to control-normal mode and the message waiting to be read includes a control part. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EINTR]</dt><dd>The read operation was terminated due to the receipt of a signal, and no data was transferred.</dd><dt>[EINVAL]</dt><dd><sup>[<a href="javascript:open_code('XSR')">XSR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The STREAM or multiplexer referenced by <i>fildes</i> is linked (directly or indirectly) downstream from a multiplexer. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EIO]</dt><dd>The process is a member of a background process attempting to read from its controlling terminal, the process is ignoring orblocking the SIGTTIN signal, or the process group is orphaned. This error may also be generated for implementation-definedreasons.</dd><dt>[EISDIR]</dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The <i>fildes</i> argument refers to a directory and the implementation does not allow the directory to be read using <i>read</i>()or <i>pread</i>(). The <a href="../functions/readdir.html"><i>readdir</i>()</a> function should be used instead. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EOVERFLOW]</dt><dd>The file is a regular file, <i>nbyte</i> is greater than 0, the starting position is before the end-of-file, and the startingposition is greater than or equal to the offset maximum established in the open file description associated with<i>fildes</i>.</dd></dl><p>The <i>read</i>() function shall fail if:</p><dl compact><dt>[EAGAIN] or [EWOULDBLOCK]</dt><dd><br>The file descriptor is for a socket, is marked O_NONBLOCK, and no data is waiting to be received.</dd><dt>[ECONNRESET]</dt><dd>A read was attempted on a socket and the connection was forcibly closed by its peer.</dd><dt>[ENOTCONN]</dt><dd>A read was attempted on a socket that is not connected.</dd><dt>[ETIMEDOUT]</dt><dd>A read was attempted on a socket and a transmission timeout occurred.</dd></dl>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -