File: Formatted-Input.html

package info (click to toggle)
octave 3.8.2-2~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 84,360 kB
  • sloc: cpp: 293,356; ansic: 42,041; fortran: 23,669; sh: 13,629; objc: 7,890; yacc: 7,093; lex: 3,442; java: 2,125; makefile: 1,589; perl: 1,009; awk: 974; xml: 34
file content (178 lines) | stat: -rw-r--r-- 9,766 bytes parent folder | download | duplicates (3)
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Formatted Input</title>

<meta name="description" content="GNU Octave: Formatted Input">
<meta name="keywords" content="GNU Octave: Formatted Input">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" 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#C_002dStyle-I_002fO-Functions" rel="up" title="C-Style I/O Functions">
<link href="Input-Conversion-Syntax.html#Input-Conversion-Syntax" rel="next" title="Input Conversion Syntax">
<link href="Other-Output-Conversions.html#Other-Output-Conversions" rel="prev" title="Other Output Conversions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {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}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Formatted-Input"></a>
<div class="header">
<p>
Next: <a href="Input-Conversion-Syntax.html#Input-Conversion-Syntax" accesskey="n" rel="next">Input Conversion Syntax</a>, Previous: <a href="Other-Output-Conversions.html#Other-Output-Conversions" accesskey="p" rel="prev">Other Output Conversions</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" 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#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Formatted-Input-1"></a>
<h4 class="subsection">14.2.11 Formatted Input</h4>

<p>Octave provides the <code>scanf</code>, <code>fscanf</code>, and <code>sscanf</code>
functions to read formatted input.  There are two forms of each of these
functions.  One can be used to extract vectors of data from a file, and
the other is more &lsquo;C-like&rsquo;.
</p>
<a name="XREFfscanf"></a><dl>
<dt><a name="index-fscanf"></a>Built-in Function: <em>[<var>val</var>, <var>count</var>, <var>errmsg</var>] =</em> <strong>fscanf</strong> <em>(<var>fid</var>, <var>template</var>, <var>size</var>)</em></dt>
<dt><a name="index-fscanf-1"></a>Built-in Function: <em>[<var>v1</var>, <var>v2</var>, &hellip;, <var>count</var>, <var>errmsg</var>] =</em> <strong>fscanf</strong> <em>(<var>fid</var>, <var>template</var>, &quot;C&quot;)</em></dt>
<dd><p>In the first form, read from <var>fid</var> according to <var>template</var>,
returning the result in the matrix <var>val</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>A string is returned if <var>template</var> specifies only character
conversions.
</p>
<p>The number of items successfully read is returned in <var>count</var>.
</p>
<p>If an error occurs, <var>errmsg</var> contains a system-dependent error message.
</p>
<p>In the second form, read from <var>fid</var> according to <var>template</var>,
with each conversion specifier in <var>template</var> corresponding to a
single scalar return value.  This form is more &ldquo;C-like&rdquo;, and also
compatible with previous versions of Octave.  The number of successful
conversions is returned in <var>count</var>
</p>
<p>See the Formatted Input section of the GNU Octave manual for a
complete description of the syntax of the template string.
</p>
<p><strong>See also:</strong> <a href="Line_002dOriented-Input.html#XREFfgets">fgets</a>, <a href="Line_002dOriented-Input.html#XREFfgetl">fgetl</a>, <a href="Binary-I_002fO.html#XREFfread">fread</a>, <a href="#XREFscanf">scanf</a>, <a href="#XREFsscanf">sscanf</a>, <a href="Opening-and-Closing-Files.html#XREFfopen">fopen</a>.
</p></dd></dl>


<a name="XREFscanf"></a><dl>
<dt><a name="index-scanf"></a>Built-in Function: <em>[<var>val</var>, <var>count</var>, <var>errmsg</var>] =</em> <strong>scanf</strong> <em>(<var>template</var>, <var>size</var>)</em></dt>
<dt><a name="index-scanf-1"></a>Built-in Function: <em>[<var>v1</var>, <var>v2</var>, &hellip;, <var>count</var>, <var>errmsg</var>]] =</em> <strong>scanf</strong> <em>(<var>template</var>, &quot;C&quot;)</em></dt>
<dd><p>This is equivalent to calling <code>fscanf</code> with <var>fid</var> = <code>stdin</code>.
</p>
<p>It is currently not useful to call <code>scanf</code> in interactive
programs.
</p>
<p><strong>See also:</strong> <a href="#XREFfscanf">fscanf</a>, <a href="#XREFsscanf">sscanf</a>, <a href="Formatted-Output.html#XREFprintf">printf</a>.
</p></dd></dl>


<a name="XREFsscanf"></a><dl>
<dt><a name="index-sscanf"></a>Built-in Function: <em>[<var>val</var>, <var>count</var>, <var>errmsg</var>, <var>pos</var>] =</em> <strong>sscanf</strong> <em>(<var>string</var>, <var>template</var>, <var>size</var>)</em></dt>
<dt><a name="index-sscanf-1"></a>Built-in Function: <em>[<var>v1</var>, <var>v2</var>, &hellip;, <var>count</var>, <var>errmsg</var>] =</em> <strong>sscanf</strong> <em>(<var>string</var>, <var>template</var>, &quot;C&quot;)</em></dt>
<dd><p>This is like <code>fscanf</code>, except that the characters are taken from the
string <var>string</var> instead of from a stream.  Reaching the end of the
string is treated as an end-of-file condition.  In addition to the values
returned by <code>fscanf</code>, the index of the next character to be read
is returned in <var>pos</var>.
</p>
<p><strong>See also:</strong> <a href="#XREFfscanf">fscanf</a>, <a href="#XREFscanf">scanf</a>, <a href="Formatted-Output.html#XREFsprintf">sprintf</a>.
</p></dd></dl>


<p>Calls to <code>scanf</code> are superficially similar to calls to
<code>printf</code> in that arbitrary arguments are read under the control of
a template string.  While the syntax of the conversion specifications in
the template is very similar to that for <code>printf</code>, the
interpretation of the template is oriented more towards free-format
input and simple pattern matching, rather than fixed-field formatting.
For example, most <code>scanf</code> conversions skip over any amount of
&ldquo;white space&rdquo; (including spaces, tabs, and newlines) in the input
file, and there is no concept of precision for the numeric input
conversions as there is for the corresponding output conversions.
Ordinarily, non-whitespace characters in the template are expected to
match characters in the input stream exactly.
<a name="index-conversion-specifications-_0028scanf_0029"></a>
</p>
<p>When a <em>matching failure</em> occurs, <code>scanf</code> returns immediately,
leaving the first non-matching character as the next character to be
read from the stream, and <code>scanf</code> returns all the items that were
successfully converted.
<a name="index-matching-failure_002c-in-scanf"></a>
</p>
<p>The formatted input functions are not used as frequently as the
formatted output functions.  Partly, this is because it takes some care
to use them properly.  Another reason is that it is difficult to recover
from a matching error.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Input-Conversion-Syntax.html#Input-Conversion-Syntax" accesskey="n" rel="next">Input Conversion Syntax</a>, Previous: <a href="Other-Output-Conversions.html#Other-Output-Conversions" accesskey="p" rel="prev">Other Output Conversions</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" 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#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>