Sophie

Sophie

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

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

<html lang="en">
<head>
<title>Binary I/O - 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="prev" href="String-Input-Conversions.html#String-Input-Conversions" title="String Input Conversions">
<link rel="next" href="Temporary-Files.html#Temporary-Files" title="Temporary Files">
<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="Binary-I%2fO"></a>
<a name="Binary-I_002fO"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Temporary-Files.html#Temporary-Files">Temporary Files</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="String-Input-Conversions.html#String-Input-Conversions">String Input Conversions</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.16 Binary I/O</h4>

<p>Octave can read and write binary data using the functions <code>fread</code>
and <code>fwrite</code>, which are patterned after the standard C functions
with the same names.  They are able to automatically swap the byte order
of integer data and convert among the supported floating point formats
as the data are read.

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

<div class="defun">
&mdash; Built-in Function: [<var>val</var>, <var>count</var>] = <b>fread</b> (<var>fid, size, precision, skip, arch</var>)<var><a name="index-fread-800"></a></var><br>
<blockquote><p>Read binary data of type <var>precision</var> from the specified file ID
<var>fid</var>.

        <p>The optional argument <var>size</var> specifies the amount of data to read
and may be one of

          <dl>
<dt><code>Inf</code><dd>Read as much as possible, returning a column vector.

          <br><dt><var>nr</var><dd>Read up to <var>nr</var> elements, returning a column vector.

          <br><dt><code>[</code><var>nr</var><code>, Inf]</code><dd>Read as much as possible, returning a matrix with <var>nr</var> rows.  If the
number of elements read is not an exact multiple of <var>nr</var>, the last
column is padded with zeros.

          <br><dt><code>[</code><var>nr</var><code>, </code><var>nc</var><code>]</code><dd>Read up to <var>nr</var><code> * </code><var>nc</var> elements, returning a matrix with
<var>nr</var> rows.  If the number of elements read is not an exact multiple
of <var>nr</var>, the last column is padded with zeros. 
</dl>

     <p class="noindent">If <var>size</var> is omitted, a value of <code>Inf</code> is assumed.

        <p>The optional argument <var>precision</var> is a string specifying the type of
data to read and may be one of

          <dl>
<dt><code>"schar"</code><dt><code>"signed char"</code><dd>Signed character.

          <br><dt><code>"uchar"</code><dt><code>"unsigned char"</code><dd>Unsigned character.

          <br><dt><code>"int8"</code><dt><code>"integer*1"</code><dd>
8-bit signed integer.

          <br><dt><code>"int16"</code><dt><code>"integer*2"</code><dd>16-bit signed integer.

          <br><dt><code>"int32"</code><dt><code>"integer*4"</code><dd>32-bit signed integer.

          <br><dt><code>"int64"</code><dt><code>"integer*8"</code><dd>64-bit signed integer.

          <br><dt><code>"uint8"</code><dd>8-bit unsigned integer.

          <br><dt><code>"uint16"</code><dd>16-bit unsigned integer.

          <br><dt><code>"uint32"</code><dd>32-bit unsigned integer.

          <br><dt><code>"uint64"</code><dd>64-bit unsigned integer.

          <br><dt><code>"single"</code><dt><code>"float32"</code><dt><code>"real*4"</code><dd>32-bit floating point number.

          <br><dt><code>"double"</code><dt><code>"float64"</code><dt><code>"real*8"</code><dd>64-bit floating point number.

          <br><dt><code>"char"</code><dt><code>"char*1"</code><dd>Single character.

          <br><dt><code>"short"</code><dd>Short integer (size is platform dependent).

          <br><dt><code>"int"</code><dd>Integer (size is platform dependent).

          <br><dt><code>"long"</code><dd>Long integer (size is platform dependent).

          <br><dt><code>"ushort"</code><dt><code>"unsigned short"</code><dd>Unsigned short integer (size is platform dependent).

          <br><dt><code>"uint"</code><dt><code>"unsigned int"</code><dd>Unsigned integer (size is platform dependent).

          <br><dt><code>"ulong"</code><dt><code>"unsigned long"</code><dd>Unsigned long integer (size is platform dependent).

          <br><dt><code>"float"</code><dd>Single precision floating point number (size is platform dependent). 
</dl>

     <p class="noindent">The default precision is <code>"uchar"</code>.

        <p>The <var>precision</var> argument may also specify an optional repeat
count.  For example, &lsquo;<samp><span class="samp">32*single</span></samp>&rsquo; causes <code>fread</code> to read
a block of 32 single precision floating point numbers.  Reading in
blocks is useful in combination with the <var>skip</var> argument.

        <p>The <var>precision</var> argument may also specify a type conversion. 
For example, &lsquo;<samp><span class="samp">int16=&gt;int32</span></samp>&rsquo; causes <code>fread</code> to read 16-bit
integer values and return an array of 32-bit integer values.  By
default, <code>fread</code> returns a double precision array.  The special
form &lsquo;<samp><span class="samp">*TYPE</span></samp>&rsquo; is shorthand for &lsquo;<samp><span class="samp">TYPE=&gt;TYPE</span></samp>&rsquo;.

        <p>The conversion and repeat counts may be combined.  For example, the
specification &lsquo;<samp><span class="samp">32*single=&gt;single</span></samp>&rsquo; causes <code>fread</code> to read
blocks of single precision floating point values and return an array
of single precision values instead of the default array of double
precision values.

        <p>The optional argument <var>skip</var> specifies the number of bytes to skip
after each element (or block of elements) is read.  If it is not
specified, a value of 0 is assumed.  If the final block read is not
complete, the final skip is omitted.  For example,

     <pre class="example">          fread (f, 10, "3*single=&gt;single", 8)
</pre>
        <p class="noindent">will omit the final 8-byte skip because the last read will not be
a complete block of 3 values.

        <p>The optional argument <var>arch</var> is a string specifying the data format
for the file.  Valid values are

          <dl>
<dt><code>"native"</code><dd>The format of the current machine.

          <br><dt><code>"ieee-be"</code><dd>IEEE big endian.

          <br><dt><code>"ieee-le"</code><dd>IEEE little endian.

          <br><dt><code>"vaxd"</code><dd>VAX D floating format.

          <br><dt><code>"vaxg"</code><dd>VAX G floating format.

          <br><dt><code>"cray"</code><dd>Cray floating format. 
</dl>

     <p class="noindent">Conversions are currently only supported for <code>"ieee-be"</code> and
<code>"ieee-le"</code> formats.

        <p>The data read from the file is returned in <var>val</var>, and the number of
values read is returned in <code>count</code>
<!-- 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_002dfwrite.html#doc_002dfwrite">fwrite</a>, <a href="doc_002dfopen.html#doc_002dfopen">fopen</a>, <a href="doc_002dfclose.html#doc_002dfclose">fclose</a>. 
</p></blockquote></div>

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

<div class="defun">
&mdash; Built-in Function: <var>count</var> = <b>fwrite</b> (<var>fid, data, precision, skip, arch</var>)<var><a name="index-fwrite-801"></a></var><br>
<blockquote><p>Write data in binary form of type <var>precision</var> to the specified file
ID <var>fid</var>, returning the number of values successfully written to the
file.

        <p>The argument <var>data</var> is a matrix of values that are to be written to
the file.  The values are extracted in column-major order.

        <p>The remaining arguments <var>precision</var>, <var>skip</var>, and <var>arch</var> are
optional, and are interpreted as described for <code>fread</code>.

        <p>The behavior of <code>fwrite</code> is undefined if the values in <var>data</var>
are too large to fit in the specified precision. 
<!-- 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_002dfread.html#doc_002dfread">fread</a>, <a href="doc_002dfopen.html#doc_002dfopen">fopen</a>, <a href="doc_002dfclose.html#doc_002dfclose">fclose</a>. 
</p></blockquote></div>

   </body></html>