📄 fdopen.html
字号:
<!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>fdopen</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="fdopen"></a> <a name="tag_03_146"></a><!-- fdopen --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_146_01"></a>NAME</h4><blockquote>fdopen - associate a stream with a file descriptor</blockquote><h4><a name="tag_03_146_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/stdio.h.html">stdio.h</a>><br><br> FILE *fdopen(int</tt> <i>fildes</i><tt>, const char *</tt><i>mode</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]"border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_146_03"></a>DESCRIPTION</h4><blockquote><p>The <i>fdopen</i>() function shall associate a stream with a file descriptor.</p><p>The <i>mode</i> argument is a character string having one of the following values:</p><dl compact><dt><i>r</i> or <i>rb</i></dt><dd>Open a file for reading.</dd><dt><i>w</i> or <i>wb</i></dt><dd>Open a file for writing.</dd><dt><i>a</i> or <i>ab</i></dt><dd>Open a file for writing at end-of-file.</dd><dt><i>r</i>+ or <i>rb</i>+ or <i>r</i>+<i>b</i></dt><dd>Open a file for update (reading and writing).</dd><dt><i>w</i>+ or <i>wb</i>+ or <i>w</i>+<i>b</i></dt><dd>Open a file for update (reading and writing).</dd><dt><i>a</i>+ or <i>ab</i>+ or <i>a</i>+<i>b</i></dt><dd>Open a file for update (reading and writing) at end-of-file.</dd></dl><p>The meaning of these flags is exactly as specified in <a href="../functions/fopen.html"><i>fopen</i>()</a>, except that modesbeginning with <i>w</i> shall not cause truncation of the file.</p><p>Additional values for the <i>mode</i> argument may be supported by an implementation.</p><p>The application shall ensure that the mode of the stream as expressed by the <i>mode</i> argument is allowed by the file accessmode of the open file description to which <i>fildes</i> refers. The file position indicator associated with the new stream is setto the position indicated by the file offset associated with the file descriptor.</p><p>The error and end-of-file indicators for the stream shall be cleared. The <i>fdopen</i>() function may cause the <i>st_atime</i>field of the underlying file to be marked for update.</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>fdopen</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>fdopen</i>() function is unspecified. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p>The <i>fdopen</i>() function shall preserve the offset maximum previously set for the open file description corresponding to<i>fildes</i>.</p></blockquote><h4><a name="tag_03_146_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>fdopen</i>() shall return a pointer to a stream; otherwise, a null pointer shall be returned and<i>errno</i> set to indicate the error.</p></blockquote><h4><a name="tag_03_146_05"></a>ERRORS</h4><blockquote><p>The <i>fdopen</i>() function may fail if:</p><dl compact><dt>[EBADF]</dt><dd>The <i>fildes</i> argument is not a valid file descriptor.</dd><dt>[EINVAL]</dt><dd>The <i>mode</i> argument is not a valid mode.</dd><dt>[EMFILE]</dt><dd>{FOPEN_MAX} streams are currently open in the calling process.</dd><dt>[EMFILE]</dt><dd>{STREAM_MAX} streams are currently open in the calling process.</dd><dt>[ENOMEM]</dt><dd>Insufficient space to allocate a buffer.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_146_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_146_07"></a>APPLICATION USAGE</h4><blockquote><p>File descriptors are obtained from calls like <a href="../functions/open.html"><i>open</i>()</a>, <a href="../functions/dup.html"><i>dup</i>()</a>, <a href="../functions/creat.html"><i>creat</i>()</a>, or <a href="../functions/pipe.html"><i>pipe</i>()</a>, which open files but do not return streams.</p></blockquote><h4><a name="tag_03_146_08"></a>RATIONALE</h4><blockquote><p>The file descriptor may have been obtained from <a href="../functions/open.html"><i>open</i>()</a>, <a href="../functions/creat.html"><i>creat</i>()</a>, <a href="../functions/pipe.html"><i>pipe</i>()</a>, <a href="../functions/dup.html"><i>dup</i>()</a>, or <a href="../functions/fcntl.html"><i>fcntl</i>()</a>; inherited through <a href="../functions/fork.html"><i>fork</i>()</a> or <i><a href="../functions/exec.html">exec</a></i>; or perhaps obtained byimplementation-defined means, such as the 4.3 BSD <a href="../functions/socket.html"><i>socket</i>()</a> call.</p><p>The meanings of the <i>mode</i> arguments of <i>fdopen</i>() and <a href="../functions/fopen.html"><i>fopen</i>()</a> differ.With <i>fdopen</i>(), open for write (<i>w</i> or <i>w+</i>) does not truncate, and append (<i>a</i> or <i>a+</i>) cannot createfor writing. The <i>mode</i> argument formats that include <i>a b</i> are allowed for consistency with the ISO C standardfunction <a href="../functions/fopen.html"><i>fopen</i>()</a>. The <i>b</i> has no effect on the resulting stream. Although notexplicitly required by this volume of IEEE Std 1003.1-2001, a good implementation of append (<i>a</i>) mode would causethe O_APPEND flag to be set.</p></blockquote><h4><a name="tag_03_146_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_146_10"></a>SEE ALSO</h4><blockquote><p><a href="xsh_chap02_05.html#tag_02_05_01"><i>Interaction of File Descriptors and Standard I/O Streams</i></a> , <a href="fclose.html"><i>fclose</i>()</a> , <a href="fopen.html"><i>fopen</i>()</a> , <a href="open.html"><i>open</i>()</a> , the BaseDefinitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/stdio.h.html"><i><stdio.h></i></a></p></blockquote><h4><a name="tag_03_146_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_146_12"></a>Issue 5</h4><blockquote><p>The DESCRIPTION is updated for alignment with the POSIX Realtime Extension.</p><p>Large File Summit extensions are added.</p></blockquote><h4><a name="tag_03_146_13"></a>Issue 6</h4><blockquote><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>In the DESCRIPTION, the use and setting of the <i>mode</i> argument are changed to include binary streams.</p></li><li><p>In the DESCRIPTION, text is added for large file support to indicate setting of the offset maximum in the open filedescription.</p></li><li><p>All errors identified in the ERRORS section are added.</p></li><li><p>In the DESCRIPTION, text is added that the <i>fdopen</i>() function may cause <i>st_atime</i> to be updated.</p></li></ul><p>The following changes were made to align with the IEEE P1003.1a draft standard:</p><ul><li><p>Clarification is added that it is the responsibility of the application to ensure that the mode is compatible with the open filedescriptor.</p></li></ul><p>The DESCRIPTION is updated for alignment with IEEE Std 1003.1j-2000 by specifying that <i>fdopen</i>() results areunspecified for typed memory objects.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -