File: Binary-I_002fO.html

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (263 lines) | stat: -rw-r--r-- 11,574 bytes parent folder | download
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Binary I/O (GNU Octave (version 6.2.0))</title>

<meta name="description" content="Binary I/O (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Binary I/O (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<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="Temporary-Files.html" rel="next" title="Temporary Files">
<link href="String-Input-Conversions.html" rel="prev" title="String Input Conversions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<span id="Binary-I_002fO"></span><div class="header">
<p>
Next: <a href="Temporary-Files.html" accesskey="n" rel="next">Temporary Files</a>, Previous: <a href="String-Input-Conversions.html" accesskey="p" rel="prev">String Input Conversions</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html" accesskey="u" rel="up">C-Style I/O Functions</a> &nbsp; [<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>
<span id="Binary-I_002fO-1"></span><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.
</p>
<span id="XREFfread"></span><dl>
<dt id="index-fread">: <em><var>val</var> =</em> <strong>fread</strong> <em>(<var>fid</var>)</em></dt>
<dt id="index-fread-1">: <em><var>val</var> =</em> <strong>fread</strong> <em>(<var>fid</var>, <var>size</var>)</em></dt>
<dt id="index-fread-2">: <em><var>val</var> =</em> <strong>fread</strong> <em>(<var>fid</var>, <var>size</var>, <var>precision</var>)</em></dt>
<dt id="index-fread-3">: <em><var>val</var> =</em> <strong>fread</strong> <em>(<var>fid</var>, <var>size</var>, <var>precision</var>, <var>skip</var>)</em></dt>
<dt id="index-fread-4">: <em><var>val</var> =</em> <strong>fread</strong> <em>(<var>fid</var>, <var>size</var>, <var>precision</var>, <var>skip</var>, <var>arch</var>)</em></dt>
<dt id="index-fread-5">: <em>[<var>val</var>, <var>count</var>] =</em> <strong>fread</strong> <em>(&hellip;)</em></dt>
<dd><p>Read binary data from the file specified by the file descriptor <var>fid</var>.
</p>
<p>The optional argument <var>size</var> specifies the amount of data to read
and may be one of
</p>
<dl compact="compact">
<dt><code>Inf</code></dt>
<dd><p>Read as much as possible, returning a column vector.
</p>
</dd>
<dt><code><var>nr</var></code></dt>
<dd><p>Read up to <var>nr</var> elements, returning a column vector.
</p>
</dd>
<dt><code>[<var>nr</var>, Inf]</code></dt>
<dd><p>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.
</p>
</dd>
<dt><code>[<var>nr</var>, <var>nc</var>]</code></dt>
<dd><p>Read up to <code><var>nr</var> * <var>nc</var></code> 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.
</p></dd>
</dl>

<p>If <var>size</var> is omitted, a value of <code>Inf</code> is assumed.
</p>
<p>The optional argument <var>precision</var> is a string specifying the type of
data to read and may be one of
</p>
<dl compact="compact">
<dt><code>&quot;uint8&quot;</code> (default)</dt>
<dd><p>8-bit unsigned integer.
</p>
</dd>
<dt><code>&quot;int8&quot;</code></dt>
<dt><code>&quot;integer*1&quot;</code></dt>
<dd><p>8-bit signed integer.
</p>
</dd>
<dt><code>&quot;uint16&quot;</code></dt>
<dt><code>&quot;ushort&quot;</code></dt>
<dt><code>&quot;unsigned short&quot;</code></dt>
<dd><p>16-bit unsigned integer.
</p>
</dd>
<dt><code>&quot;int16&quot;</code></dt>
<dt><code>&quot;integer*2&quot;</code></dt>
<dt><code>&quot;short&quot;</code></dt>
<dd><p>16-bit signed integer.
</p>
</dd>
<dt><code>&quot;uint&quot;</code></dt>
<dt><code>&quot;uint32&quot;</code></dt>
<dt><code>&quot;unsigned int&quot;</code></dt>
<dt><code>&quot;ulong&quot;</code></dt>
<dt><code>&quot;unsigned long&quot;</code></dt>
<dd><p>32-bit unsigned integer.
</p>
</dd>
<dt><code>&quot;int&quot;</code></dt>
<dt><code>&quot;int32&quot;</code></dt>
<dt><code>&quot;integer*4&quot;</code></dt>
<dt><code>&quot;long&quot;</code></dt>
<dd><p>32-bit signed integer.
</p>
</dd>
<dt><code>&quot;uint64&quot;</code></dt>
<dd><p>64-bit unsigned integer.
</p>
</dd>
<dt><code>&quot;int64&quot;</code></dt>
<dt><code>&quot;integer*8&quot;</code></dt>
<dd><p>64-bit signed integer.
</p>
</dd>
<dt><code>&quot;single&quot;</code></dt>
<dt><code>&quot;float&quot;</code></dt>
<dt><code>&quot;float32&quot;</code></dt>
<dt><code>&quot;real*4&quot;</code></dt>
<dd><p>32-bit floating point number.
</p>
</dd>
<dt><code>&quot;double&quot;</code></dt>
<dt><code>&quot;float64&quot;</code></dt>
<dt><code>&quot;real*8&quot;</code></dt>
<dd><p>64-bit floating point number.
</p>
</dd>
<dt><code>&quot;char&quot;</code></dt>
<dt><code>&quot;char*1&quot;</code></dt>
<dd><p>8-bit single character.
</p>
</dd>
<dt><code>&quot;uchar&quot;</code></dt>
<dt><code>&quot;unsigned char&quot;</code></dt>
<dd><p>8-bit unsigned character.
</p>
</dd>
<dt><code>&quot;schar&quot;</code></dt>
<dt><code>&quot;signed char&quot;</code></dt>
<dd><p>8-bit signed character.
</p>
</dd>
</dl>

<p>The default precision is <code>&quot;uint8&quot;</code>.
</p>
<p>The <var>precision</var> argument may also specify an optional repeat
count.  For example, &lsquo;<samp>32*single</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>
<p>The <var>precision</var> argument may also specify a type conversion.
For example, &lsquo;<samp>int16=&gt;int32</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>*TYPE</samp>&rsquo; is shorthand for &lsquo;<samp>TYPE=&gt;TYPE</samp>&rsquo;.
</p>
<p>The conversion and repeat counts may be combined.  For example, the
specification &lsquo;<samp>32*single=&gt;single</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>
<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,
</p>
<div class="example">
<pre class="example">fread (f, 10, &quot;3*single=&gt;single&quot;, 8)
</pre></div>

<p>will omit the final 8-byte skip because the last read will not be
a complete block of 3 values.
</p>
<p>The optional argument <var>arch</var> is a string specifying the data format
for the file.  Valid values are
</p>
<dl compact="compact">
<dt><code>&quot;native&quot;</code> or <code>&quot;n&quot;</code></dt>
<dd><p>The format of the current machine.
</p>
</dd>
<dt><code>&quot;ieee-be&quot;</code> or <code>&quot;b&quot;</code></dt>
<dd><p>IEEE big endian.
</p>
</dd>
<dt><code>&quot;ieee-le&quot;</code> or <code>&quot;l&quot;</code></dt>
<dd><p>IEEE little endian.
</p></dd>
</dl>

<p>If no <var>arch</var> is given the value used in the call to <code>fopen</code> which
created the file descriptor is used.  Otherwise, the value specified with
<code>fread</code> overrides that of <code>fopen</code> and determines the data format.
</p>
<p>The output argument <var>val</var> contains the data read from the file.
</p>
<p>The optional return value <var>count</var> contains the number of elements read.
</p>
<p><strong>See also:</strong> <a href="#XREFfwrite">fwrite</a>, <a href="Line_002dOriented-Input.html#XREFfgets">fgets</a>, <a href="Line_002dOriented-Input.html#XREFfgetl">fgetl</a>, <a href="Formatted-Input.html#XREFfscanf">fscanf</a>, <a href="Opening-and-Closing-Files.html#XREFfopen">fopen</a>.
</p></dd></dl>


<span id="XREFfwrite"></span><dl>
<dt id="index-fwrite">: <em></em> <strong>fwrite</strong> <em>(<var>fid</var>, <var>data</var>)</em></dt>
<dt id="index-fwrite-1">: <em></em> <strong>fwrite</strong> <em>(<var>fid</var>, <var>data</var>, <var>precision</var>)</em></dt>
<dt id="index-fwrite-2">: <em></em> <strong>fwrite</strong> <em>(<var>fid</var>, <var>data</var>, <var>precision</var>, <var>skip</var>)</em></dt>
<dt id="index-fwrite-3">: <em></em> <strong>fwrite</strong> <em>(<var>fid</var>, <var>data</var>, <var>precision</var>, <var>skip</var>, <var>arch</var>)</em></dt>
<dt id="index-fwrite-4">: <em><var>count</var> =</em> <strong>fwrite</strong> <em>(&hellip;)</em></dt>
<dd><p>Write data in binary form to the file specified by the file descriptor
<var>fid</var>, returning the number of values <var>count</var> successfully written
to the file.
</p>
<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>
<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>
<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.
</p>
<p><strong>See also:</strong> <a href="#XREFfread">fread</a>, <a href="Simple-Output.html#XREFfputs">fputs</a>, <a href="Formatted-Output.html#XREFfprintf">fprintf</a>, <a href="Opening-and-Closing-Files.html#XREFfopen">fopen</a>.
</p></dd></dl>


<hr>
<div class="header">
<p>
Next: <a href="Temporary-Files.html" accesskey="n" rel="next">Temporary Files</a>, Previous: <a href="String-Input-Conversions.html" accesskey="p" rel="prev">String Input Conversions</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html" accesskey="u" rel="up">C-Style I/O Functions</a> &nbsp; [<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>