Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/freopen.html
2024-02-19 00:21:47 -05:00

327 lines
14 KiB
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 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-2001<br>
Copyright &copy; 2001 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 &lt;<a href="../basedefs/stdio.h.html">stdio.h</a>&gt;<br>
<br>
FILE *freopen(const char *restrict</tt> <i>filename</i><tt>, const char *restrict</tt> <i>mode</i><tt>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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&nbsp;C standard. Any conflict between the
requirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers to
the ISO&nbsp;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 indicators
for 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 the
stream 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 that
specified by <i>mode</i>, as if the name of the file currently associated with the stream had been used. It is
implementation-defined which 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"> &nbsp;the encoding
rule shall be cleared, <img src="../images/opt-end.gif" alt="[Option End]" border="0"> and the associated <b>mbstate_t</b> object
shall 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"> The
largest value that can be represented correctly in an object of type <b>off_t</b> shall be established as the offset maximum in the
open 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 be
returned, <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
"0"> &nbsp;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> are
denied, 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>[EINTR]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> A
signal 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"> A
loop 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"> A
component 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"> A
component 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. <img
src="../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>. <img
src="../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>[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"> A
request 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 &lt;stdio.h&gt;
...
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&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/stdio.h.html"><i>&lt;stdio.h&gt;</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 is
set 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&nbsp;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 to
support 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&nbsp;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 &quot;file&quot; to &quot;file descriptor&quot;.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; 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>