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

380 lines
16 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>fopen</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="fopen"></a> <a name="tag_03_176"></a><!-- fopen -->
<!--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_176_01"></a>NAME</h4>
<blockquote>fopen - open a stream</blockquote>
<h4><a name="tag_03_176_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 *fopen(const char *restrict</tt> <i>filename</i><tt>, const char *restrict</tt> <i>mode</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_176_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>fopen</i>() function shall open the file whose pathname is the string pointed to by <i>filename</i>, and associates a
stream with it.</p>
<p>The <i>mode</i> argument points to a string. If the string is one of the following, the file shall be opened in the indicated
mode. Otherwise, the behavior is undefined.</p>
<dl compact>
<dt><i>r</i>&nbsp;or&nbsp;<i>rb</i></dt>
<dd>Open file for reading.</dd>
<dt><i>w</i>&nbsp;or&nbsp;<i>wb</i></dt>
<dd>Truncate to zero length or create file for writing.</dd>
<dt><i>a</i>&nbsp;or&nbsp;<i>ab</i></dt>
<dd>Append; open or create file for writing at end-of-file.</dd>
<dt><i>r+</i>&nbsp;or&nbsp;<i>rb+</i>&nbsp;or&nbsp;<i>r+b</i></dt>
<dd>Open file for update (reading and writing).</dd>
<dt><i>w+</i>&nbsp;or&nbsp;<i>wb+</i>&nbsp;or&nbsp;<i>w+b</i></dt>
<dd>Truncate to zero length or create file for update.</dd>
<dt><i>a+</i>&nbsp;or&nbsp;<i>ab+</i>&nbsp;or&nbsp;<i>a+b</i></dt>
<dd>Append; open or create file for update, writing at end-of-file.</dd>
</dl>
<p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The
character <tt>'b'</tt> shall have no effect, but is allowed for ISO&nbsp;C standard conformance. <img src="../images/opt-end.gif"
alt="[Option End]" border="0"> Opening a file with read mode (<i>r</i> as the first character in the <i>mode</i> argument) shall
fail if the file does not exist or cannot be read.</p>
<p>Opening a file with append mode (<i>a</i> as the first character in the <i>mode</i> argument) shall cause all subsequent writes
to the file to be forced to the then current end-of-file, regardless of intervening calls to <a href=
"../functions/fseek.html"><i>fseek</i>()</a>.</p>
<p>When a file is opened with update mode ( <tt>'+'</tt> as the second or third character in the <i>mode</i> argument), both input
and output may be performed on the associated stream. However, the application shall ensure that output is not directly followed by
input without an intervening call to <a href="../functions/fflush.html"><i>fflush</i>()</a> or to a file positioning function ( <a
href="../functions/fseek.html"><i>fseek</i>()</a>, <a href="../functions/fsetpos.html"><i>fsetpos</i>()</a>, or <a href=
"../functions/rewind.html"><i>rewind</i>()</a>), and input is not directly followed by output without an intervening call to a file
positioning function, unless the input operation encounters end-of-file.</p>
<p>When opened, a stream is fully buffered if and only if it can be determined not to refer to an interactive device. The error and
end-of-file indicators for the stream shall be cleared.</p>
<p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> If
<i>mode</i> is <i>w</i>, <i>wb</i>, <i>a</i>, <i>ab</i>, <i>w</i>+, <i>wb</i>+, <i>w</i>+<i>b</i>, <i>a</i>+, <i>ab</i>+, or
<i>a</i>+<i>b</i>, and the file did not previously exist, upon successful completion, the <i>fopen</i>() function shall mark for
update the <i>st_atime</i>, <i>st_ctime</i>, and <i>st_mtime</i> fields of the file and the <i>st_ctime</i> and <i>st_mtime</i>
fields of the parent directory.</p>
<p>If <i>mode</i> is <i>w</i>, <i>wb</i>, <i>w</i>+, <i>wb</i>+, or <i>w</i>+<i>b</i>, and the file did previously exist, upon
successful completion, <i>fopen</i>() shall mark for update the <i>st_ctime</i> and <i>st_mtime</i> fields of the file. The
<i>fopen</i>() function shall allocate a file descriptor as <a href="../functions/open.html"><i>open</i>()</a> does. <img src=
"../images/opt-end.gif" alt="[Option End]" border="0"></p>
<p>After a successful call to the <i>fopen</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_176_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>fopen</i>() shall return a pointer to the object controlling the stream. 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_176_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>fopen</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>fopen</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 the file 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>fopen</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>[EMFILE]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
{FOPEN_MAX} streams are currently open in the calling process. <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">
{STREAM_MAX} streams 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>
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>[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_176_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_176_06_01"></a>Opening a File</h5>
<p>The following example tries to open the file named <b>file</b> for reading. The <i>fopen</i>() function returns a file pointer
that is used in subsequent <a href="../functions/fgets.html"><i>fgets</i>()</a> and <a href=
"../functions/fclose.html"><i>fclose</i>()</a> calls. If the program cannot open the file, it just ignores it.</p>
<pre>
<tt>#include &lt;stdio.h&gt;
...
FILE *fp;
...
void rgrep(const char *file)
{
...
if ((fp = fopen(file, "r")) == NULL)
return;
...
}
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_176_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_176_08"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_176_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_176_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="fclose.html"><i>fclose</i>()</a> , <a href="fdopen.html"><i>fdopen</i>()</a> , <a href=
"freopen.html"><i>freopen</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_176_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_176_12"></a>Issue 5</h4>
<blockquote>
<p>Large File Summit extensions are added.</p>
</blockquote>
<h4><a name="tag_03_176_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>The [EINVAL], [EMFILE], [ENAMETOOLONG], [ENOMEM], and [ETXTBSY] optional error conditions are added.</p>
</li>
</ul>
<p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; for application requirements.</p>
<p>The following changes are made for alignment with the ISO/IEC&nbsp;9899:1999 standard:</p>
<ul>
<li>
<p>The prototype for <i>fopen</i>() is updated.</p>
</li>
<li>
<p>The DESCRIPTION is updated to note that if the argument <i>mode</i> points to a string other than those listed, then the
behavior is undefined.</p>
</li>
</ul>
<p>The wording of the mandatory [ELOOP] error condition is updated, and a second optional [ELOOP] error condition is added.</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>