📄 freopen.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-2004 IEEE and The Open Group, All Rights Reserved --><title>freopen</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="freopen"></a> <a name="tag_03_188"></a><!-- freopen --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_188_01"></a>NAME</h4><blockquote>freopen - open a stream</blockquote><h4><a name="tag_03_188_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include <<a href="../basedefs/stdio.h.html">stdio.h</a>><br><br> FILE *freopen(const char *restrict</tt> <i>filename</i><tt>, const char *restrict</tt> <i>mode</i><tt>,<br> FILE *restrict</tt> <i>stream</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_188_03"></a>DESCRIPTION</h4><blockquote><div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"border="0"> The functionality described on this reference page is aligned with the ISO C standard. Any conflict between therequirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers tothe ISO C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div><p>The <i>freopen</i>() function shall first attempt to flush the stream and close any file descriptor associated with<i>stream</i>. Failure to flush or close the file descriptor successfully shall be ignored. The error and end-of-file indicatorsfor the stream shall be cleared.</p><p>The <i>freopen</i>() function shall open the file whose pathname is the string pointed to by <i>filename</i> and associate thestream pointed to by <i>stream</i> with it. The <i>mode</i> argument shall be used just as in <a href="../functions/fopen.html"><i>fopen</i>()</a>.</p><p>The original stream shall be closed regardless of whether the subsequent open succeeds.</p><p>If <i>filename</i> is a null pointer, the <i>freopen</i>() function shall attempt to change the mode of the stream to thatspecified by <i>mode</i>, as if the name of the file currently associated with the stream had been used. In this case, the filedescriptor associated with the stream need not be closed if the call to <i>freopen</i>() succeeds. It is implementation-definedwhich changes of mode are permitted (if any), and under what circumstances.</p><p>After a successful call to the <i>freopen</i>() function, the orientation of the stream shall be cleared, <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> the encodingrule shall be cleared, <img src="../images/opt-end.gif" alt="[Option End]" border="0"> and the associated <b>mbstate_t</b> objectshall be set to describe an initial conversion state.</p><p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Thelargest value that can be represented correctly in an object of type <b>off_t</b> shall be established as the offset maximum in theopen file description. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_188_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>freopen</i>() shall return the value of <i>stream</i>. Otherwise, a null pointer shall bereturned, <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> and <i>errno</i> shall be set to indicate the error. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_188_05"></a>ERRORS</h4><blockquote><p>The <i>freopen</i>() function shall fail if:</p><dl compact><dt>[EACCES]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by <i>mode</i> aredenied, or the file does not exist and write permission is denied for the parent directory of the file to be created. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EBADF]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The file descriptor underlying the stream is not a valid file descriptor when <i>filename</i> is a null pointer. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EINTR]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Asignal was caught during <i>freopen</i>(). <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EISDIR]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The named file is a directory and <i>mode</i> requires write access. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ELOOP]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Aloop exists in symbolic links encountered during resolution of the <i>path</i> argument. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EMFILE]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">{OPEN_MAX} file descriptors are currently open in the calling process. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ENAMETOOLONG]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"><br>The length of the <i>filename</i> argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ENFILE]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The maximum allowable number of files is currently open in the system. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ENOENT]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Acomponent of <i>filename</i> does not name an existing file or <i>filename</i> is an empty string. <img src="../images/opt-end.gif"alt="[Option End]" border="0"></dd><dt>[ENOSPC]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The directory or file system that would contain the new file cannot be expanded, the file does not exist, and it was to be created.<img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ENOTDIR]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Acomponent of the path prefix is not a directory. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ENXIO]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The named file is a character special or block special file, and the device associated with this special file does not exist. <imgsrc="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EOVERFLOW]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The named file is a regular file and the size of the file cannot be represented correctly in an object of type <b>off_t</b>. <imgsrc="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EROFS]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The named file resides on a read-only file system and <i>mode</i> requires write access. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl><p>The <i>freopen</i>() function may fail if:</p><dl compact><dt>[EBADF]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The mode with which the file descriptor underlying the stream was opened does not support the requested mode when <i>filename</i>is a null pointer. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EINVAL]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The value of the <i>mode</i> argument is not valid. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ELOOP]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the <i>path</i> argument. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ENAMETOOLONG]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"><br>Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ENOMEM]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Insufficient storage space is available. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ENXIO]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Arequest was made of a nonexistent device, or the request was outside the capabilities of the device. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[ETXTBSY]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The file is a pure procedure (shared text) file that is being executed and <i>mode</i> requires write access. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_188_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_188_06_01"></a>Directing Standard Output to a File</h5><p>The following example logs all standard output to the <b>/tmp/logfile</b> file.</p><pre><tt>#include <stdio.h>...FILE *fp;...fp = freopen ("/tmp/logfile", "a+", stdout);...</tt></pre></blockquote><h4><a name="tag_03_188_07"></a>APPLICATION USAGE</h4><blockquote><p>The <i>freopen</i>() function is typically used to attach the preopened <i>streams</i> associated with <i>stdin</i>,<i>stdout</i>, and <i>stderr</i> to other files.</p></blockquote><h4><a name="tag_03_188_08"></a>RATIONALE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_188_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_188_10"></a>SEE ALSO</h4><blockquote><p><a href="fclose.html"><i>fclose</i>()</a>, <a href="fopen.html"><i>fopen</i>()</a>, <a href="fdopen.html"><i>fdopen</i>()</a>, <a href="mbsinit.html"><i>mbsinit</i>()</a>, the Base Definitions 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_188_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_188_12"></a>Issue 5</h4><blockquote><p>The DESCRIPTION is updated to indicate that the orientation of the stream is cleared and the conversion state of the stream isset to an initial conversion state by a successful call to the <i>freopen</i>() function.</p><p>Large File Summit extensions are added.</p></blockquote><h4><a name="tag_03_188_13"></a>Issue 6</h4><blockquote><p>Extensions beyond the ISO C standard are marked.</p><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>In the DESCRIPTION, text is added to indicate setting of the offset maximum in the open file description. This change is tosupport large files.</p></li><li><p>In the ERRORS section, the [EOVERFLOW] condition is added. This change is to support large files.</p></li><li><p>The [ELOOP] mandatory error condition is added.</p></li><li><p>A second [ENAMETOOLONG] is added as an optional error condition.</p></li><li><p>The [EINVAL], [ENOMEM], [ENXIO], and [ETXTBSY] optional error conditions are added.</p></li></ul><p>The following changes are made for alignment with the ISO/IEC 9899:1999 standard:</p><ul><li><p>The <i>freopen</i>() prototype is updated.</p></li><li><p>The DESCRIPTION is updated.</p></li></ul><p>The wording of the mandatory [ELOOP] error condition is updated, and a second optional [ELOOP] error condition is added.</p><p>The DESCRIPTION is updated regarding failure to close, changing the "file" to "file descriptor".</p><p>IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/40 is applied, adding the sentence "In this case, the filedescriptor associated with the stream need not be closed if the call to <i>freopen</i>() succeeds." to the DESCRIPTION.</p><p>IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/41 is applied, adding an mandatory [EBADF] error, and anoptional [EBADF] error to the ERRORS section.</p></blockquote><div class="box"><em>End of informative text.</em></div><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 + -