📄 fopen.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>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, 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_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 <<a href="../basedefs/stdio.h.html">stdio.h</a>><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 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>fopen</i>() function shall open the file whose pathname is the string pointed to by <i>filename</i>, and associates astream 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 indicatedmode. Otherwise, the behavior is undefined.</p><dl compact><dt><i>r</i> or <i>rb</i></dt><dd>Open file for reading.</dd><dt><i>w</i> or <i>wb</i></dt><dd>Truncate to zero length or create file for writing.</dd><dt><i>a</i> or <i>ab</i></dt><dd>Append; open or create file for writing at end-of-file.</dd><dt><i>r+</i> or <i>rb+</i> or <i>r+b</i></dt><dd>Open file for update (reading and writing).</dd><dt><i>w+</i> or <i>wb+</i> or <i>w+b</i></dt><dd>Truncate to zero length or create file for update.</dd><dt><i>a+</i> or <i>ab+</i> or <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"> Thecharacter <tt>'b'</tt> shall have no effect, but is allowed for ISO 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) shallfail 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 writesto 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 inputand output may be performed on the associated stream. However, the application shall ensure that output is not directly followed byinput without an intervening call to <a href="../functions/fflush.html"><i>fflush</i>()</a> or to a file positioning function ( <ahref="../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 filepositioning 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 andend-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 forupdate 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, uponsuccessful 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"> 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_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 nullpointer shall be returned, <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_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> 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>[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>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"> 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 the file was to becreated. <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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -