Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 301

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Opening and Closing Files - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" title="C-Style I/O Functions">
<link rel="next" href="Simple-Output.html#Simple-Output" title="Simple Output">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="Opening-and-Closing-Files"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Simple-Output.html#Simple-Output">Simple Output</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions">C-Style I/O Functions</a>
<hr>
</div>

<h4 class="subsection">14.2.1 Opening and Closing Files</h4>

<p>When reading data from a file it must be opened for reading first, and
likewise when writing to a file.  The <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>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.

<pre class="example">     filename = "myfile.txt";
     fid = fopen (filename, "w");
     # Do the actual I/O here...
     fclose (fid);
</pre>
   <!-- file-io.cc -->
   <p><a name="doc_002dfopen"></a>

<div class="defun">
&mdash; Built-in Function: [<var>fid</var>, <var>msg</var>] = <b>fopen</b> (<var>name, mode, arch</var>)<var><a name="index-fopen-773"></a></var><br>
&mdash; Built-in Function: <var>fid_list</var> = <b>fopen</b> (<var>"all"</var>)<var><a name="index-fopen-774"></a></var><br>
&mdash; Built-in Function: [<var>file</var>, <var>mode</var>, <var>arch</var>] = <b>fopen</b> (<var>fid</var>)<var><a name="index-fopen-775"></a></var><br>
<blockquote><p>The first form of the <code>fopen</code> function opens the named file with
the specified mode (read-write, read-only, etc.) and architecture
interpretation (IEEE big endian, IEEE little endian, etc.), and returns
an integer value that may be used to refer to the file later.  If an
error occurs, <var>fid</var> is set to &minus;1 and <var>msg</var> contains the
corresponding system error message.  The <var>mode</var> is a one or two
character string that specifies whether the file is to be opened for
reading, writing, or both.

        <p>The second form of the <code>fopen</code> function returns a vector of file ids
corresponding to all the currently open files, excluding the
<code>stdin</code>, <code>stdout</code>, and <code>stderr</code> streams.

        <p>The third form of the <code>fopen</code> function returns information about the
open file given its file id.

        <p>For example,

     <pre class="example">          myfile = fopen ("splat.dat", "r", "ieee-le");
</pre>
        <p class="noindent">opens the file <samp><span class="file">splat.dat</span></samp> for reading.  If necessary, binary
numeric values will be read assuming they are stored in IEEE format with
the least significant bit first, and then converted to the native
representation.

        <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>The possible values &lsquo;<samp><span class="samp">mode</span></samp>&rsquo; may have are

          <dl>
<dt>&lsquo;<samp><span class="samp">r</span></samp>&rsquo;<dd>Open a file for reading.

          <br><dt>&lsquo;<samp><span class="samp">w</span></samp>&rsquo;<dd>Open a file for writing.  The previous contents are discarded.

          <br><dt>&lsquo;<samp><span class="samp">a</span></samp>&rsquo;<dd>Open or create a file for writing at the end of the file.

          <br><dt>&lsquo;<samp><span class="samp">r+</span></samp>&rsquo;<dd>Open an existing file for reading and writing.

          <br><dt>&lsquo;<samp><span class="samp">w+</span></samp>&rsquo;<dd>Open a file for reading or writing.  The previous contents are
discarded.

          <br><dt>&lsquo;<samp><span class="samp">a+</span></samp>&rsquo;<dd>Open or create a file for reading or writing at the end of the
file. 
</dl>

        <p>Append a "t" to the mode string to open the file in text mode or a
"b" to open in binary mode.  On Windows and Macintosh systems, text
mode reading and writing automatically converts linefeeds to the
appropriate line end character for the system (carriage-return linefeed
on Windows, carriage-return on Macintosh).  The default if no mode is
specified is binary mode.

        <p>Additionally, you may append a "z" 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>The parameter <var>arch</var> is a string specifying the default data format
for the file.  Valid values for <var>arch</var> are:

          <dl>
&lsquo;<samp><span class="samp">native</span></samp>&rsquo;
The format of the current machine (this is the default).

          <p>&lsquo;<samp><span class="samp">ieee-be</span></samp>&rsquo;
IEEE big endian format.

          <p>&lsquo;<samp><span class="samp">ieee-le</span></samp>&rsquo;
IEEE little endian format.

          <p>&lsquo;<samp><span class="samp">vaxd</span></samp>&rsquo;
VAX D floating format.

          <p>&lsquo;<samp><span class="samp">vaxg</span></samp>&rsquo;
VAX G floating format.

          <p>&lsquo;<samp><span class="samp">cray</span></samp>&rsquo;
Cray floating format. 
</dl>

     <p class="noindent">however, conversions are currently only supported for &lsquo;<samp><span class="samp">native</span></samp>&rsquo;
&lsquo;<samp><span class="samp">ieee-be</span></samp>&rsquo;, and &lsquo;<samp><span class="samp">ieee-le</span></samp>&rsquo; formats. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dfclose.html#doc_002dfclose">fclose</a>, <a href="doc_002dfgets.html#doc_002dfgets">fgets</a>, <a href="doc_002dfputs.html#doc_002dfputs">fputs</a>, <a href="doc_002dfread.html#doc_002dfread">fread</a>, <a href="doc_002dfseek.html#doc_002dfseek">fseek</a>, <a href="doc_002dferror.html#doc_002dferror">ferror</a>, <a href="doc_002dfprintf.html#doc_002dfprintf">fprintf</a>, <a href="doc_002dfscanf.html#doc_002dfscanf">fscanf</a>, <a href="doc_002dftell.html#doc_002dftell">ftell</a>, <a href="doc_002dfwrite.html#doc_002dfwrite">fwrite</a>. 
</p></blockquote></div>

<!-- file-io.cc -->
   <p><a name="doc_002dfclose"></a>

<div class="defun">
&mdash; Built-in Function:  <b>fclose</b> (<var>fid</var>)<var><a name="index-fclose-776"></a></var><br>
<blockquote><p>Closes the specified file.  If successful, <code>fclose</code> returns 0,
otherwise, it returns -1. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dfopen.html#doc_002dfopen">fopen</a>, <a href="doc_002dfseek.html#doc_002dfseek">fseek</a>, <a href="doc_002dftell.html#doc_002dftell">ftell</a>. 
</p></blockquote></div>

   </body></html>