1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Opening and Closing Files (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Opening and Closing Files (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Opening and Closing Files (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="C_002dStyle-I_002fO-Functions.html" rel="up" title="C-Style I/O Functions">
<link href="Simple-Output.html" rel="next" title="Simple Output">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Opening-and-Closing-Files">
<div class="nav-panel">
<p>
Next: <a href="Simple-Output.html" accesskey="n" rel="next">Simple Output</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html" accesskey="u" rel="up">C-Style I/O Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Opening-and-Closing-Files-1"><span>14.2.1 Opening and Closing Files<a class="copiable-link" href="#Opening-and-Closing-Files-1"> ¶</a></span></h4>
<p>When reading data from a file it must be opened for reading first, and
likewise when writing to a file. The <code class="code">fopen</code> function returns a
pointer to an open file that is ready to be read or written. Once all
data has been read from or written to the opened file it should be closed.
The <code class="code">fclose</code> function does this. The following code illustrates
the basic pattern for writing to a file, but a very similar pattern is
used when reading a file.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">filename = "myfile.txt";
fid = fopen (filename, "w");
# Do the actual I/O here...
fclose (fid);
</pre></div></div>
<a class="anchor" id="XREFfopen"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-fopen"><span><code class="def-type"><var class="var">fid</var> =</code> <strong class="def-name">fopen</strong> <code class="def-code-arguments">(<var class="var">name</var>)</code><a class="copiable-link" href="#index-fopen"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fopen-1"><span><code class="def-type"><var class="var">fid</var> =</code> <strong class="def-name">fopen</strong> <code class="def-code-arguments">(<var class="var">name</var>, <var class="var">mode</var>)</code><a class="copiable-link" href="#index-fopen-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fopen-2"><span><code class="def-type"><var class="var">fid</var> =</code> <strong class="def-name">fopen</strong> <code class="def-code-arguments">(<var class="var">name</var>, <var class="var">mode</var>, <var class="var">arch</var>)</code><a class="copiable-link" href="#index-fopen-2"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fopen-3"><span><code class="def-type"><var class="var">fid</var> =</code> <strong class="def-name">fopen</strong> <code class="def-code-arguments">(<var class="var">name</var>, <var class="var">mode</var>, <var class="var">arch</var>, <var class="var">encoding</var>)</code><a class="copiable-link" href="#index-fopen-3"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fopen-4"><span><code class="def-type">[<var class="var">fid</var>, <var class="var">msg</var>] =</code> <strong class="def-name">fopen</strong> <code class="def-code-arguments">(…)</code><a class="copiable-link" href="#index-fopen-4"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fopen-5"><span><code class="def-type"><var class="var">fid_list</var> =</code> <strong class="def-name">fopen</strong> <code class="def-code-arguments">("all")</code><a class="copiable-link" href="#index-fopen-5"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fopen-6"><span><code class="def-type">[<var class="var">file</var>, <var class="var">mode</var>, <var class="var">arch</var>, <var class="var">encoding</var>] =</code> <strong class="def-name">fopen</strong> <code class="def-code-arguments">(<var class="var">fid</var>)</code><a class="copiable-link" href="#index-fopen-6"> ¶</a></span></dt>
<dd><p>Open a file for low-level I/O or query open files and file descriptors.
</p>
<p>The first form of the <code class="code">fopen</code> function opens the named file with
the specified mode (read-write, read-only, etc.), architecture
interpretation (IEEE big endian, IEEE little endian, etc.) and file encoding,
and returns an integer value that may be used to refer to the file later. If
an error occurs, <var class="var">fid</var> is set to −1 and <var class="var">msg</var> contains the
corresponding system error message. The <var class="var">mode</var> is a one or two
character string that specifies whether the file is to be opened for
reading, writing, or both. The <var class="var">encoding</var> is a character string with a
valid encoding identifier. This encoding is used when strings are read from
or written to the file. By default, that is UTF-8.
</p>
<p>The second form of the <code class="code">fopen</code> function returns a vector of file ids
corresponding to all the currently open files, excluding the
<code class="code">stdin</code>, <code class="code">stdout</code>, and <code class="code">stderr</code> streams.
</p>
<p>The third form of the <code class="code">fopen</code> function returns information about the
open file given its file id.
</p>
<p>For example,
</p>
<div class="example">
<pre class="example-preformatted">myfile = fopen ("splat.dat", "r", "ieee-le");
</pre></div>
<p>opens the file <samp class="file">splat.dat</samp> for reading. If necessary, binary
numeric values will be read assuming they are stored in IEEE 754 format
with the least significant bit first, and then converted to the native
representation.
</p>
<p>Opening a file that is already open simply opens it again and returns a
separate file id. It is not an error to open a file several times,
though writing to the same file through several different file ids may
produce unexpected results.
</p>
<p>The possible values of <var class="var">mode</var> are
</p>
<dl class="table">
<dt>‘<samp class="samp">r</samp>’ (default)</dt>
<dd><p>Open a file for reading.
</p>
</dd>
<dt>‘<samp class="samp">w</samp>’</dt>
<dd><p>Open a file for writing. The previous contents are discarded.
</p>
</dd>
<dt>‘<samp class="samp">a</samp>’</dt>
<dd><p>Open or create a file for writing at the end of the file.
</p>
</dd>
<dt>‘<samp class="samp">r+</samp>’</dt>
<dd><p>Open an existing file for reading and writing.
</p>
</dd>
<dt>‘<samp class="samp">w+</samp>’</dt>
<dd><p>Open a file for reading or writing. The previous contents are
discarded.
</p>
</dd>
<dt>‘<samp class="samp">a+</samp>’</dt>
<dd><p>Open or create a file for reading or writing at the end of the
file.
</p></dd>
</dl>
<p>Append a <code class="code">"t"</code> to the mode string to open the file in text mode or a
<code class="code">"b"</code> to open in binary mode. On Windows systems,
text mode reading and writing automatically converts linefeeds to the
appropriate line end character for the system (carriage-return linefeed on
Windows). The default when no mode is specified is binary.
</p>
<p>Additionally, you may append a <code class="code">"z"</code> to the mode string to open a
gzipped file for reading or writing. For this to be successful, you
must also open the file in binary mode.
</p>
<p>The parameter <var class="var">arch</var> is a string specifying the default data format
for the file. Valid values for <var class="var">arch</var> are:
</p>
<dl class="table">
<dt><code class="code">"native"</code> or <code class="code">"n"</code> (default)</dt>
<dd><p>The format of the current machine.
</p>
</dd>
<dt><code class="code">"ieee-be"</code> or <code class="code">"b"</code></dt>
<dd><p>IEEE big endian format.
</p>
</dd>
<dt><code class="code">"ieee-le"</code> or <code class="code">"l"</code></dt>
<dd><p>IEEE little endian format.
</p></dd>
</dl>
<p>When opening a new file that does not yet exist, permissions will be set to
<code class="code">0666 - <var class="var">umask</var></code>.
</p>
<p>Compatibility Note: Octave opens files using buffered I/O. Small writes are
accumulated until an internal buffer is filled, and then everything is written
in a single operation. This is very efficient and improves performance.
<small class="sc">MATLAB</small>, however, opens files using flushed I/O where every write operation
is immediately performed. If the write operation must be performed immediately
after data has been written then the write should be followed by a call to
<code class="code">fflush</code> to flush the internal buffer.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFfclose">fclose</a>, <a class="ref" href="Line_002dOriented-Input.html#XREFfgets">fgets</a>, <a class="ref" href="Line_002dOriented-Input.html#XREFfgetl">fgetl</a>, <a class="ref" href="Formatted-Input.html#XREFfscanf">fscanf</a>, <a class="ref" href="Binary-I_002fO.html#XREFfread">fread</a>, <a class="ref" href="Simple-Output.html#XREFfputs">fputs</a>, <a class="ref" href="Simple-File-I_002fO.html#XREFfdisp">fdisp</a>, <a class="ref" href="Formatted-Output.html#XREFfprintf">fprintf</a>, <a class="ref" href="Binary-I_002fO.html#XREFfwrite">fwrite</a>, <a class="ref" href="Line_002dOriented-Input.html#XREFfskipl">fskipl</a>, <a class="ref" href="File-Positioning.html#XREFfseek">fseek</a>, <a class="ref" href="File-Positioning.html#XREFfrewind">frewind</a>, <a class="ref" href="File-Positioning.html#XREFftell">ftell</a>, <a class="ref" href="EOF-and-Errors.html#XREFfeof">feof</a>, <a class="ref" href="EOF-and-Errors.html#XREFferror">ferror</a>, <a class="ref" href="EOF-and-Errors.html#XREFfclear">fclear</a>, <a class="ref" href="Paging-Screen-Output.html#XREFfflush">fflush</a>, <a class="ref" href="EOF-and-Errors.html#XREFfreport">freport</a>, <a class="ref" href="Filesystem-Utilities.html#XREFumask">umask</a>.
</p></dd></dl>
<a class="anchor" id="XREFfclose"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-fclose"><span><code class="def-type"><var class="var">status</var> =</code> <strong class="def-name">fclose</strong> <code class="def-code-arguments">(<var class="var">fid</var>)</code><a class="copiable-link" href="#index-fclose"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fclose-1"><span><code class="def-type"><var class="var">status</var> =</code> <strong class="def-name">fclose</strong> <code class="def-code-arguments">("all")</code><a class="copiable-link" href="#index-fclose-1"> ¶</a></span></dt>
<dd><p>Close the file specified by the file descriptor <var class="var">fid</var>.
</p>
<p>If successful, <code class="code">fclose</code> returns 0, otherwise, it returns -1. The
second form of the <code class="code">fclose</code> call closes all open files except
<code class="code">stdin</code>, <code class="code">stdout</code>, <code class="code">stderr</code>, and any FIDs associated
with gnuplot.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFfopen">fopen</a>, <a class="ref" href="Paging-Screen-Output.html#XREFfflush">fflush</a>, <a class="ref" href="EOF-and-Errors.html#XREFfreport">freport</a>.
</p></dd></dl>
<a class="anchor" id="XREFis_005fvalid_005ffile_005fid"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-is_005fvalid_005ffile_005fid"><span><code class="def-type"><var class="var">tf</var> =</code> <strong class="def-name">is_valid_file_id</strong> <code class="def-code-arguments">(<var class="var">fid</var>)</code><a class="copiable-link" href="#index-is_005fvalid_005ffile_005fid"> ¶</a></span></dt>
<dd><p>Return true if <var class="var">fid</var> refers to an open file.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="EOF-and-Errors.html#XREFfreport">freport</a>, <a class="ref" href="#XREFfopen">fopen</a>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Simple-Output.html">Simple Output</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html">C-Style I/O Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|