Mercurial > octave
annotate doc/interpreter/io.txi @ 33661:4c378dd47cf2 default tip @
command-widget: Use new signal-slot syntax for better compiler diagnostics.
* libgui/src/command-widget.cc (console::console): Use new signal-slot syntax
for better compiler diagnostics.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Wed, 12 Jun 2024 17:24:20 +0200 |
parents | 2e484f9f1f18 |
children |
rev | line source |
---|---|
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
31706
diff
changeset
|
1 @c Copyright (C) 1996-2024 The Octave Project Developers |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24520
diff
changeset
|
5 @c Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
6 @c under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24520
diff
changeset
|
7 @c the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 @c (at your option) any later version. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
9 @c |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
10 @c Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
11 @c WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 @c GNU General Public License for more details. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
14 @c |
7018 | 15 @c You should have received a copy of the GNU General Public License |
16 @c along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24520
diff
changeset
|
17 @c <https://www.gnu.org/licenses/>. |
3294 | 18 |
4169 | 19 @node Input and Output |
3294 | 20 @chapter Input and Output |
21 | |
6666 | 22 Octave supports several ways of reading and writing data to or from the |
8828 | 23 prompt or a file. The simplest functions for data Input and Output |
12546
39ca02387a32
Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents:
12522
diff
changeset
|
24 (I/O) are easy to use, but only provide limited control of how |
16826
a4969508008e
doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
25 data is processed. For more control, a set of functions modeled |
6666 | 26 after the C standard library are also provided by Octave. |
27 | |
28 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
29 * Basic Input and Output:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
30 * C-Style I/O Functions:: |
6666 | 31 @end menu |
32 | |
33 @node Basic Input and Output | |
34 @section Basic Input and Output | |
35 | |
36 @c We could use a two-line introduction here... | |
37 | |
38 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
39 * Terminal Output:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
40 * Terminal Input:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
41 * Simple File I/O:: |
6666 | 42 @end menu |
43 | |
44 @node Terminal Output | |
45 @subsection Terminal Output | |
46 | |
47 Since Octave normally prints the value of an expression as soon as it | |
48 has been evaluated, the simplest of all I/O functions is a simple | |
49 expression. For example, the following expression will display the | |
50 value of @samp{pi} | |
51 | |
52 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
53 @group |
6666 | 54 pi |
28652
bb0ca2753bc2
update docs for built-in variables (bug #58988)
John W. Eaton <jwe@octave.org>
parents:
27796
diff
changeset
|
55 @print{} ans = 3.1416 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
56 @end group |
6666 | 57 @end example |
58 | |
59 This works well as long as it is acceptable to have the name of the | |
60 variable (or @samp{ans}) printed along with the value. To print the | |
61 value of a variable without printing its name, use the function | |
62 @code{disp}. | |
63 | |
64 The @code{format} command offers some control over the way Octave prints | |
65 values with @code{disp} and through the normal echoing mechanism. | |
66 | |
67 @DOCSTRING(disp) | |
68 | |
12562
c686d2be0102
Add list_in_columns, terminal_size functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12546
diff
changeset
|
69 @DOCSTRING(list_in_columns) |
c686d2be0102
Add list_in_columns, terminal_size functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12546
diff
changeset
|
70 |
c686d2be0102
Add list_in_columns, terminal_size functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12546
diff
changeset
|
71 @DOCSTRING(terminal_size) |
c686d2be0102
Add list_in_columns, terminal_size functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12546
diff
changeset
|
72 |
6666 | 73 @DOCSTRING(format) |
74 | |
75 @menu | |
76 * Paging Screen Output:: | |
77 @end menu | |
78 | |
79 @node Paging Screen Output | |
80 @subsubsection Paging Screen Output | |
81 | |
24801
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
82 When running interactively, Octave normally sends all output directly to the |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
83 Command Window. However, when using the CLI version of Octave this can create |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
84 a problem because large volumes of data will stream by before you can read |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
85 them. In such cases, it is better to use a paging program such as @code{less} |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
86 or @code{more} which displays just one screenful at a time. With @code{less} |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
87 (and some versions of @code{more}) you can also scan forward and backward, and |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
88 search for specific items. The pager is enabled by the command @code{more on}. |
3294 | 89 |
24801
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
90 Normally, no output is displayed by the pager until just before Octave is ready |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
91 to print the top level prompt, or read from the standard input (for example, by |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
92 using the @code{fscanf} or @code{scanf} functions). This means that there may |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
93 be some delay before any output appears on your screen if you have asked Octave |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
94 to perform a significant amount of work with a single command statement. The |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
95 function @code{fflush} may be used to force output to be sent to the pager (or |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
96 any other stream) immediately. |
3294 | 97 |
24801
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
98 You can select the program to run as the pager with the @code{PAGER} function, |
daf61c7dfcae
Change default status of pager to "off" (bug #53217).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
99 and configure the pager itself with the @code{PAGER_FLAGS} function. |
3294 | 100 |
3372 | 101 @DOCSTRING(more) |
3294 | 102 |
3372 | 103 @DOCSTRING(PAGER) |
3294 | 104 |
6549 | 105 @DOCSTRING(PAGER_FLAGS) |
106 | |
3372 | 107 @DOCSTRING(page_screen_output) |
3294 | 108 |
3372 | 109 @DOCSTRING(page_output_immediately) |
3294 | 110 |
3372 | 111 @DOCSTRING(fflush) |
3294 | 112 |
21092
429f83903400
maint: Use "FIXME:" more places in code base.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
113 @c FIXME: maybe this would be a good place to describe the following message: |
3294 | 114 @c |
115 @c warning: connection to external pager (pid = 9334) lost -- | |
116 @c warning: pending computations and output may be lost | |
117 @c warning: broken pipe | |
118 | |
4167 | 119 @node Terminal Input |
3294 | 120 @subsection Terminal Input |
121 | |
122 Octave has three functions that make it easy to prompt users for | |
123 input. The @code{input} and @code{menu} functions are normally | |
124 used for managing an interactive dialog with a user, and the | |
125 @code{keyboard} function is normally used for doing simple debugging. | |
126 | |
3372 | 127 @DOCSTRING(input) |
3294 | 128 |
3372 | 129 @DOCSTRING(menu) |
3294 | 130 |
8817
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8569
diff
changeset
|
131 @DOCSTRING(yes_or_no) |
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8569
diff
changeset
|
132 |
6646 | 133 For @code{input}, the normal command line history and editing functions |
134 are available at the prompt. | |
3294 | 135 |
136 Octave also has a function that makes it possible to get a single | |
137 character from the keyboard without requiring the user to type a | |
138 carriage return. | |
139 | |
3372 | 140 @DOCSTRING(kbhit) |
3294 | 141 |
4167 | 142 @node Simple File I/O |
3294 | 143 @subsection Simple File I/O |
144 | |
5225 | 145 @cindex saving data |
146 @cindex loading data | |
3294 | 147 The @code{save} and @code{load} commands allow data to be written to and |
148 read from disk files in various formats. The default format of files | |
6666 | 149 written by the @code{save} command can be controlled using the functions |
16875
b04ae15530fc
Rename default_save_options() to save_default_options().
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
150 @code{save_default_options} and @code{save_precision}. |
6666 | 151 |
152 As an example the following code creates a 3-by-3 matrix and saves it | |
153 to the file @samp{myfile.mat}. | |
154 | |
155 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
156 @group |
6666 | 157 A = [ 1:3; 4:6; 7:9 ]; |
158 save myfile.mat A | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
159 @end group |
6666 | 160 @end example |
3294 | 161 |
6666 | 162 Once one or more variables have been saved to a file, they can be |
163 read into memory using the @code{load} command. | |
164 | |
165 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
166 @group |
6666 | 167 load myfile.mat |
168 A | |
169 @print{} A = | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
170 @print{} |
6666 | 171 @print{} 1 2 3 |
172 @print{} 4 5 6 | |
173 @print{} 7 8 9 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
174 @end group |
6666 | 175 @end example |
3294 | 176 |
3372 | 177 @DOCSTRING(save) |
3294 | 178 |
6666 | 179 There are three functions that modify the behavior of @code{save}. |
180 | |
16875
b04ae15530fc
Rename default_save_options() to save_default_options().
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
181 @DOCSTRING(save_default_options) |
6666 | 182 |
183 @DOCSTRING(save_precision) | |
184 | |
185 @DOCSTRING(save_header_format_string) | |
186 | |
16214
ee041a93c755
put save functions text after save command
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
15466
diff
changeset
|
187 @DOCSTRING(load) |
ee041a93c755
put save functions text after save command
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
15466
diff
changeset
|
188 |
ee041a93c755
put save functions text after save command
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
15466
diff
changeset
|
189 @DOCSTRING(fileread) |
ee041a93c755
put save functions text after save command
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
15466
diff
changeset
|
190 |
6666 | 191 @DOCSTRING(native_float_format) |
192 | |
8828 | 193 It is possible to write data to a file in a similar way to the |
6666 | 194 @code{disp} function for writing data to the screen. The @code{fdisp} |
195 works just like @code{disp} except its first argument is a file pointer | |
196 as created by @code{fopen}. As an example, the following code writes | |
197 to data @samp{myfile.txt}. | |
198 | |
199 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
200 @group |
6666 | 201 fid = fopen ("myfile.txt", "w"); |
202 fdisp (fid, "3/8 is "); | |
203 fdisp (fid, 3/8); | |
204 fclose (fid); | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
205 @end group |
6666 | 206 @end example |
207 | |
208 @noindent | |
209 @xref{Opening and Closing Files}, for details on how to use @code{fopen} | |
210 and @code{fclose}. | |
211 | |
212 @DOCSTRING(fdisp) | |
213 | |
7580
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
214 Octave can also read and write matrices text files such as comma |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
215 separated lists. |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
216 |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
217 @DOCSTRING(dlmwrite) |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
218 |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
219 @DOCSTRING(dlmread) |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
220 |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
221 @DOCSTRING(csvwrite) |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
222 |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
223 @DOCSTRING(csvread) |
b4aa9ef3d3ef
Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
224 |
11148 | 225 Formatted data from can be read from, or written to, text files as well. |
226 | |
11147
a81994607ca0
document textscan and textread
John W. Eaton <jwe@octave.org>
parents:
11142
diff
changeset
|
227 @DOCSTRING(textread) |
a81994607ca0
document textscan and textread
John W. Eaton <jwe@octave.org>
parents:
11142
diff
changeset
|
228 |
a81994607ca0
document textscan and textread
John W. Eaton <jwe@octave.org>
parents:
11142
diff
changeset
|
229 @DOCSTRING(textscan) |
a81994607ca0
document textscan and textread
John W. Eaton <jwe@octave.org>
parents:
11142
diff
changeset
|
230 |
15547
9a455cf96dbe
Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
231 The @code{importdata} function has the ability to work with a wide |
9a455cf96dbe
Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
232 variety of data. |
9a455cf96dbe
Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
233 |
9a455cf96dbe
Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
234 @DOCSTRING(importdata) |
9a455cf96dbe
Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
235 |
27763 | 236 After importing, the data may need to be transformed before further analysis. |
237 The @code{rescale} function can shift and normalize a data set to a specified | |
238 range. | |
27756
bd40796683d8
rescale.m: New function (bug #56639).
Christian Himpe <christian.himpe@wwu.de>
parents:
27384
diff
changeset
|
239 |
bd40796683d8
rescale.m: New function (bug #56639).
Christian Himpe <christian.himpe@wwu.de>
parents:
27384
diff
changeset
|
240 @DOCSTRING(rescale) |
bd40796683d8
rescale.m: New function (bug #56639).
Christian Himpe <christian.himpe@wwu.de>
parents:
27384
diff
changeset
|
241 |
6666 | 242 @menu |
243 * Saving Data on Unexpected Exits:: | |
244 @end menu | |
245 | |
246 @node Saving Data on Unexpected Exits | |
247 @subsubsection Saving Data on Unexpected Exits | |
248 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8286
diff
changeset
|
249 If Octave for some reason exits unexpectedly it will by default save the |
6666 | 250 variables available in the workspace to a file in the current directory. |
14889
577df411e0c7
rename octave-core file to octave-workspace
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
251 By default this file is named @samp{octave-workspace} and can be loaded |
9039
51dc9691f23f
Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents:
9032
diff
changeset
|
252 into memory with the @code{load} command. While the default behavior |
6666 | 253 most often is reasonable it can be changed through the following |
254 functions. | |
3294 | 255 |
3372 | 256 @DOCSTRING(crash_dumps_octave_core) |
3294 | 257 |
4449 | 258 @DOCSTRING(sighup_dumps_octave_core) |
259 | |
24520
c5c11b07598a
refactor signal handling (bug #52757)
John W. Eaton <jwe@octave.org>
parents:
23966
diff
changeset
|
260 @DOCSTRING(sigquit_dumps_octave_core) |
c5c11b07598a
refactor signal handling (bug #52757)
John W. Eaton <jwe@octave.org>
parents:
23966
diff
changeset
|
261 |
4449 | 262 @DOCSTRING(sigterm_dumps_octave_core) |
263 | |
5287 | 264 @DOCSTRING(octave_core_file_options) |
3294 | 265 |
6550 | 266 @DOCSTRING(octave_core_file_limit) |
267 | |
268 @DOCSTRING(octave_core_file_name) | |
269 | |
4167 | 270 @node C-Style I/O Functions |
3294 | 271 @section C-Style I/O Functions |
272 | |
273 Octave's C-style input and output functions provide most of the | |
274 functionality of the C programming language's standard I/O library. The | |
275 argument lists for some of the input functions are slightly different, | |
276 however, because Octave has no way of passing arguments by reference. | |
277 | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
20101
diff
changeset
|
278 In the following, @var{file} refers to a filename and @code{fid} refers |
3294 | 279 to an integer file number, as returned by @code{fopen}. |
280 | |
281 There are three files that are always available. Although these files | |
282 can be accessed using their corresponding numeric file ids, you should | |
283 always use the symbolic names given in the table below, since it will | |
284 make your programs easier to understand. | |
285 | |
3372 | 286 @DOCSTRING(stdin) |
3294 | 287 |
3372 | 288 @DOCSTRING(stdout) |
3294 | 289 |
3372 | 290 @DOCSTRING(stderr) |
3294 | 291 |
292 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
293 * Opening and Closing Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
294 * Simple Output:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
295 * Line-Oriented Input:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
296 * Formatted Output:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
297 * Output Conversion for Matrices:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
298 * Output Conversion Syntax:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
299 * Table of Output Conversions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
300 * Integer Conversions:: |
9032
349616d9c38e
Cleanup top-level documentation menu in octave.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
301 * Floating-Point Conversions:: |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
302 * Other Output Conversions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
303 * Formatted Input:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
304 * Input Conversion Syntax:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
305 * Table of Input Conversions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
306 * Numeric Input Conversions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
307 * String Input Conversions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
308 * Binary I/O:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
309 * Temporary Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
310 * EOF and Errors:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
311 * File Positioning:: |
3294 | 312 @end menu |
313 | |
4167 | 314 @node Opening and Closing Files |
3294 | 315 @subsection Opening and Closing Files |
316 | |
6666 | 317 When reading data from a file it must be opened for reading first, and |
318 likewise when writing to a file. The @code{fopen} function returns a | |
319 pointer to an open file that is ready to be read or written. Once all | |
320 data has been read from or written to the opened file it should be closed. | |
321 The @code{fclose} function does this. The following code illustrates | |
322 the basic pattern for writing to a file, but a very similar pattern is | |
323 used when reading a file. | |
324 | |
325 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
326 @group |
6666 | 327 filename = "myfile.txt"; |
328 fid = fopen (filename, "w"); | |
9039
51dc9691f23f
Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents:
9032
diff
changeset
|
329 # Do the actual I/O here@dots{} |
6666 | 330 fclose (fid); |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
331 @end group |
6666 | 332 @end example |
333 | |
3372 | 334 @DOCSTRING(fopen) |
3294 | 335 |
3372 | 336 @DOCSTRING(fclose) |
3294 | 337 |
11142
3450551f591e
new function, is_valid_file_id
John W. Eaton <jwe@octave.org>
parents:
10828
diff
changeset
|
338 @DOCSTRING(is_valid_file_id) |
3450551f591e
new function, is_valid_file_id
John W. Eaton <jwe@octave.org>
parents:
10828
diff
changeset
|
339 |
4167 | 340 @node Simple Output |
3294 | 341 @subsection Simple Output |
342 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
343 Once a file has been opened for writing a string can be written to the |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
344 file using the @code{fputs} function. The following example shows |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
345 how to write the string @samp{Free Software is needed for Free Science} |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
346 to the file @samp{free.txt}. |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
347 |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
348 @example |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
349 @group |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
350 filename = "free.txt"; |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
351 fid = fopen (filename, "w"); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
352 fputs (fid, "Free Software is needed for Free Science"); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
353 fclose (fid); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
354 @end group |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
355 @end example |
6666 | 356 |
3372 | 357 @DOCSTRING(fputs) |
3294 | 358 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
359 A function much similar to @code{fputs} is available for writing data |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
360 to the screen. The @code{puts} function works just like @code{fputs} |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
361 except it doesn't take a file pointer as its input. |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
362 |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
363 @DOCSTRING(puts) |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
364 |
4167 | 365 @node Line-Oriented Input |
3294 | 366 @subsection Line-Oriented Input |
367 | |
6666 | 368 To read from a file it must be opened for reading using @code{fopen}. |
369 Then a line can be read from the file using @code{fgetl} as the following | |
370 code illustrates | |
371 | |
372 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
373 @group |
6666 | 374 fid = fopen ("free.txt"); |
375 txt = fgetl (fid) | |
376 @print{} Free Software is needed for Free Science | |
377 fclose (fid); | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
378 @end group |
6666 | 379 @end example |
380 | |
381 @noindent | |
382 This of course assumes that the file @samp{free.txt} exists and contains | |
383 the line @samp{Free Software is needed for Free Science}. | |
384 | |
3372 | 385 @DOCSTRING(fgetl) |
3294 | 386 |
3372 | 387 @DOCSTRING(fgets) |
3294 | 388 |
9701 | 389 @DOCSTRING(fskipl) |
390 | |
4167 | 391 @node Formatted Output |
3294 | 392 @subsection Formatted Output |
393 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
394 This section describes how to call @code{printf} and related functions. |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
395 |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
396 The following functions are available for formatted output. They are |
16826
a4969508008e
doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
397 modeled after the C language functions of the same name, but they |
3294 | 398 interpret the format template differently in order to improve the |
399 performance of printing vector and matrix values. | |
400 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
401 Implementation Note: For compatibility with @sc{matlab}, escape sequences in |
29117
10a35049bad7
doc: Cleanup Texinfo macros use in documentation.
Rik <rik@octave.org>
parents:
28652
diff
changeset
|
402 the template string (e.g., @qcode{"@backslashchar{}n"} => newline) are |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
403 expanded even when the template string is defined with single quotes. |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
404 |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
405 @DOCSTRING(printf) |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
406 |
3372 | 407 @DOCSTRING(fprintf) |
3294 | 408 |
3372 | 409 @DOCSTRING(sprintf) |
3294 | 410 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
411 The @code{printf} function can be used to print any number of arguments. |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
412 The template string argument you supply in a call provides |
3294 | 413 information not only about the number of additional arguments, but also |
414 about their types and what style should be used for printing them. | |
415 | |
416 Ordinary characters in the template string are simply written to the | |
417 output stream as-is, while @dfn{conversion specifications} introduced by | |
418 a @samp{%} character in the template cause subsequent arguments to be | |
419 formatted and written to the output stream. For example, | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
420 @cindex conversion specifications (@code{printf}) |
3294 | 421 |
6670 | 422 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
423 @group |
3294 | 424 pct = 37; |
425 filename = "foo.txt"; | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
426 printf ("Processed %d%% of '%s'.\nPlease be patient.\n", |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
427 pct, filename); |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
428 @end group |
6670 | 429 @end example |
3294 | 430 |
431 @noindent | |
432 produces output like | |
433 | |
6670 | 434 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
435 @group |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
436 Processed 37% of 'foo.txt'. |
3294 | 437 Please be patient. |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
438 @end group |
6670 | 439 @end example |
3294 | 440 |
441 This example shows the use of the @samp{%d} conversion to specify that a | |
442 scalar argument should be printed in decimal notation, the @samp{%s} | |
443 conversion to specify printing of a string argument, and the @samp{%%} | |
444 conversion to print a literal @samp{%} character. | |
445 | |
446 There are also conversions for printing an integer argument as an | |
447 unsigned value in octal, decimal, or hexadecimal radix (@samp{%o}, | |
448 @samp{%u}, or @samp{%x}, respectively); or as a character value | |
449 (@samp{%c}). | |
450 | |
451 Floating-point numbers can be printed in normal, fixed-point notation | |
452 using the @samp{%f} conversion or in exponential notation using the | |
453 @samp{%e} conversion. The @samp{%g} conversion uses either @samp{%e} | |
454 or @samp{%f} format, depending on what is more appropriate for the | |
455 magnitude of the particular number. | |
456 | |
457 You can control formatting more precisely by writing @dfn{modifiers} | |
458 between the @samp{%} and the character that indicates which conversion | |
459 to apply. These slightly alter the ordinary behavior of the conversion. | |
460 For example, most conversion specifications permit you to specify a | |
461 minimum field width and a flag indicating whether you want the result | |
462 left- or right-justified within the field. | |
463 | |
464 The specific flags and modifiers that are permitted and their | |
465 interpretation vary depending on the particular conversion. They're all | |
466 described in more detail in the following sections. | |
467 | |
4167 | 468 @node Output Conversion for Matrices |
3294 | 469 @subsection Output Conversion for Matrices |
470 | |
471 When given a matrix value, Octave's formatted output functions cycle | |
472 through the format template until all the values in the matrix have been | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
9701
diff
changeset
|
473 printed. For example: |
3294 | 474 |
475 @example | |
476 @group | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
477 printf ("%4.2f %10.2e %8.4g\n", hilb (3)); |
3294 | 478 |
479 @print{} 1.00 5.00e-01 0.3333 | |
480 @print{} 0.50 3.33e-01 0.25 | |
481 @print{} 0.33 2.50e-01 0.2 | |
482 @end group | |
483 @end example | |
484 | |
485 If more than one value is to be printed in a single call, the output | |
486 functions do not return to the beginning of the format template when | |
487 moving on from one value to the next. This can lead to confusing output | |
488 if the number of elements in the matrices are not exact multiples of the | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
9701
diff
changeset
|
489 number of conversions in the format template. For example: |
3294 | 490 |
491 @example | |
492 @group | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
493 printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]); |
3294 | 494 |
495 @print{} 1.00 2.00e+00 3 | |
496 @print{} 4.00 | |
497 @end group | |
498 @end example | |
499 | |
500 If this is not what you want, use a series of calls instead of just one. | |
501 | |
4167 | 502 @node Output Conversion Syntax |
3294 | 503 @subsection Output Conversion Syntax |
504 | |
505 This section provides details about the precise syntax of conversion | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
506 specifications that can appear in a @code{printf} template |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
507 string. |
3294 | 508 |
509 Characters in the template string that are not part of a | |
510 conversion specification are printed as-is to the output stream. | |
511 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
512 The conversion specifications in a @code{printf} template string have |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
513 the general form: |
3294 | 514 |
6670 | 515 @example |
3294 | 516 % @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion} |
6670 | 517 @end example |
3294 | 518 |
519 For example, in the conversion specifier @samp{%-10.8ld}, the @samp{-} | |
520 is a flag, @samp{10} specifies the field width, the precision is | |
521 @samp{8}, the letter @samp{l} is a type modifier, and @samp{d} specifies | |
522 the conversion style. (This particular type specifier says to print a | |
523 numeric argument in decimal notation, with a minimum of 8 digits | |
524 left-justified in a field at least 10 characters wide.) | |
525 | |
526 In more detail, output conversion specifications consist of an | |
527 initial @samp{%} character followed in sequence by: | |
528 | |
529 @itemize @bullet | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
530 @item |
3294 | 531 Zero or more @dfn{flag characters} that modify the normal behavior of |
532 the conversion specification. | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
533 @cindex flag character (@code{printf}) |
3294 | 534 |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
535 @item |
3294 | 536 An optional decimal integer specifying the @dfn{minimum field width}. |
537 If the normal conversion produces fewer characters than this, the field | |
538 is padded with spaces to the specified width. This is a @emph{minimum} | |
539 value; if the normal conversion produces more characters than this, the | |
540 field is @emph{not} truncated. Normally, the output is right-justified | |
541 within the field. | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
542 @cindex minimum field width (@code{printf}) |
3294 | 543 |
544 You can also specify a field width of @samp{*}. This means that the | |
545 next argument in the argument list (before the actual value to be | |
546 printed) is used as the field width. The value is rounded to the | |
547 nearest integer. If the value is negative, this means to set the | |
548 @samp{-} flag (see below) and to use the absolute value as the field | |
549 width. | |
550 | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
551 @item |
3294 | 552 An optional @dfn{precision} to specify the number of digits to be |
553 written for the numeric conversions. If the precision is specified, it | |
554 consists of a period (@samp{.}) followed optionally by a decimal integer | |
555 (which defaults to zero if omitted). | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
556 @cindex precision (@code{printf}) |
3294 | 557 |
558 You can also specify a precision of @samp{*}. This means that the next | |
559 argument in the argument list (before the actual value to be printed) is | |
560 used as the precision. The value must be an integer, and is ignored | |
561 if it is negative. | |
562 | |
563 @item | |
564 An optional @dfn{type modifier character}. This character is ignored by | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
565 Octave's @code{printf} function, but is recognized to provide |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
566 compatibility with the C language @code{printf}. |
3294 | 567 |
568 @item | |
569 A character that specifies the conversion to be applied. | |
570 @end itemize | |
571 | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
572 The exact options that are permitted and how they are interpreted vary |
3294 | 573 between the different conversion specifiers. See the descriptions of the |
574 individual conversions for information about the particular options that | |
575 they use. | |
576 | |
4167 | 577 @node Table of Output Conversions |
3294 | 578 @subsection Table of Output Conversions |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
579 @cindex output conversions, for @code{printf} |
3294 | 580 |
581 Here is a table summarizing what all the different conversions do: | |
582 | |
583 @table @asis | |
584 @item @samp{%d}, @samp{%i} | |
585 Print an integer as a signed decimal number. @xref{Integer | |
586 Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
587 output, but are different when used with @code{scanf} for input |
3294 | 588 (@pxref{Table of Input Conversions}). |
589 | |
590 @item @samp{%o} | |
591 Print an integer as an unsigned octal number. @xref{Integer | |
592 Conversions}, for details. | |
593 | |
594 @item @samp{%u} | |
595 Print an integer as an unsigned decimal number. @xref{Integer | |
596 Conversions}, for details. | |
597 | |
598 @item @samp{%x}, @samp{%X} | |
599 Print an integer as an unsigned hexadecimal number. @samp{%x} uses | |
14038
b0cdd60db5e5
doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13943
diff
changeset
|
600 lowercase letters and @samp{%X} uses uppercase. @xref{Integer |
3294 | 601 Conversions}, for details. |
602 | |
603 @item @samp{%f} | |
604 Print a floating-point number in normal (fixed-point) notation. | |
605 @xref{Floating-Point Conversions}, for details. | |
606 | |
607 @item @samp{%e}, @samp{%E} | |
608 Print a floating-point number in exponential notation. @samp{%e} uses | |
14038
b0cdd60db5e5
doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13943
diff
changeset
|
609 lowercase letters and @samp{%E} uses uppercase. @xref{Floating-Point |
3294 | 610 Conversions}, for details. |
611 | |
612 @item @samp{%g}, @samp{%G} | |
613 Print a floating-point number in either normal (fixed-point) or | |
614 exponential notation, whichever is more appropriate for its magnitude. | |
14038
b0cdd60db5e5
doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13943
diff
changeset
|
615 @samp{%g} uses lowercase letters and @samp{%G} uses uppercase. |
3294 | 616 @xref{Floating-Point Conversions}, for details. |
617 | |
618 @item @samp{%c} | |
619 Print a single character. @xref{Other Output Conversions}. | |
620 | |
621 @item @samp{%s} | |
622 Print a string. @xref{Other Output Conversions}. | |
623 | |
624 @item @samp{%%} | |
625 Print a literal @samp{%} character. @xref{Other Output Conversions}. | |
626 @end table | |
627 | |
628 If the syntax of a conversion specification is invalid, unpredictable | |
19442
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
629 things will happen, so don't do this. In particular, @sc{matlab} allows |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
630 a bare percentage sign @samp{%} with no subsequent conversion character. |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
631 Octave will emit an error and stop if it sees such code. When the string |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
632 variable to be processed cannot be guaranteed to be free of potential format |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
633 codes it is better to use the two argument form of any of the @code{printf} |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
634 functions and set the format string to @code{%s}. Alternatively, for code |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
635 which is not required to be backwards-compatible with @sc{matlab} the |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
636 Octave function @code{puts} or @code{disp} can be used. |
19442
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
637 |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
638 @example |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
639 @group |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
640 printf (strvar); # Unsafe if strvar contains format codes |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
641 printf ("%s", strvar); # Safe |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
642 puts (strvar); # Safe |
19442
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
643 @end group |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
644 @end example |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
645 |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
646 If there aren't enough function arguments provided to supply values for all |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
647 the conversion specifications in the template string, or if the arguments are |
8ee14c64ab5f
Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents:
19278
diff
changeset
|
648 not of the correct types, the results are unpredictable. If you supply more |
3294 | 649 arguments than conversion specifications, the extra argument values are |
650 simply ignored; this is sometimes useful. | |
651 | |
4167 | 652 @node Integer Conversions |
3294 | 653 @subsection Integer Conversions |
654 | |
655 This section describes the options for the @samp{%d}, @samp{%i}, | |
656 @samp{%o}, @samp{%u}, @samp{%x}, and @samp{%X} conversion | |
657 specifications. These conversions print integers in various formats. | |
658 | |
659 The @samp{%d} and @samp{%i} conversion specifications both print an | |
660 numeric argument as a signed decimal number; while @samp{%o}, | |
661 @samp{%u}, and @samp{%x} print the argument as an unsigned octal, | |
662 decimal, or hexadecimal number (respectively). The @samp{%X} conversion | |
663 specification is just like @samp{%x} except that it uses the characters | |
664 @samp{ABCDEF} as digits instead of @samp{abcdef}. | |
665 | |
666 The following flags are meaningful: | |
667 | |
668 @table @asis | |
669 @item @samp{-} | |
670 Left-justify the result in the field (instead of the normal | |
671 right-justification). | |
672 | |
673 @item @samp{+} | |
674 For the signed @samp{%d} and @samp{%i} conversions, print a | |
675 plus sign if the value is positive. | |
676 | |
677 @item @samp{ } | |
678 For the signed @samp{%d} and @samp{%i} conversions, if the result | |
679 doesn't start with a plus or minus sign, prefix it with a space | |
680 character instead. Since the @samp{+} flag ensures that the result | |
681 includes a sign, this flag is ignored if you supply both of them. | |
682 | |
683 @item @samp{#} | |
684 For the @samp{%o} conversion, this forces the leading digit to be | |
685 @samp{0}, as if by increasing the precision. For @samp{%x} or | |
686 @samp{%X}, this prefixes a leading @samp{0x} or @samp{0X} (respectively) | |
687 to the result. This doesn't do anything useful for the @samp{%d}, | |
688 @samp{%i}, or @samp{%u} conversions. | |
689 | |
690 @item @samp{0} | |
691 Pad the field with zeros instead of spaces. The zeros are placed after | |
692 any indication of sign or base. This flag is ignored if the @samp{-} | |
693 flag is also specified, or if a precision is specified. | |
694 @end table | |
695 | |
696 If a precision is supplied, it specifies the minimum number of digits to | |
697 appear; leading zeros are produced if necessary. If you don't specify a | |
698 precision, the number is printed with as many digits as it needs. If | |
699 you convert a value of zero with an explicit precision of zero, then no | |
700 characters at all are produced. | |
701 | |
4167 | 702 @node Floating-Point Conversions |
3294 | 703 @subsection Floating-Point Conversions |
704 | |
705 This section discusses the conversion specifications for floating-point | |
706 numbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G} | |
707 conversions. | |
708 | |
709 The @samp{%f} conversion prints its argument in fixed-point notation, | |
710 producing output of the form | |
711 @w{[@code{-}]@var{ddd}@code{.}@var{ddd}}, | |
712 where the number of digits following the decimal point is controlled | |
713 by the precision you specify. | |
714 | |
715 The @samp{%e} conversion prints its argument in exponential notation, | |
716 producing output of the form | |
717 @w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}. | |
718 Again, the number of digits following the decimal point is controlled by | |
719 the precision. The exponent always contains at least two digits. The | |
720 @samp{%E} conversion is similar but the exponent is marked with the letter | |
721 @samp{E} instead of @samp{e}. | |
722 | |
723 The @samp{%g} and @samp{%G} conversions print the argument in the style | |
724 of @samp{%e} or @samp{%E} (respectively) if the exponent would be less | |
725 than -4 or greater than or equal to the precision; otherwise they use the | |
726 @samp{%f} style. Trailing zeros are removed from the fractional portion | |
727 of the result and a decimal-point character appears only if it is | |
728 followed by a digit. | |
729 | |
730 The following flags can be used to modify the behavior: | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
731 @c Not @samp so we can have ' ' as an item. |
3294 | 732 |
733 @table @asis | |
734 @item @samp{-} | |
735 Left-justify the result in the field. Normally the result is | |
736 right-justified. | |
737 | |
738 @item @samp{+} | |
739 Always include a plus or minus sign in the result. | |
740 | |
741 @item @samp{ } | |
742 If the result doesn't start with a plus or minus sign, prefix it with a | |
743 space instead. Since the @samp{+} flag ensures that the result includes | |
744 a sign, this flag is ignored if you supply both of them. | |
745 | |
746 @item @samp{#} | |
747 Specifies that the result should always include a decimal point, even | |
748 if no digits follow it. For the @samp{%g} and @samp{%G} conversions, | |
749 this also forces trailing zeros after the decimal point to be left | |
750 in place where they would otherwise be removed. | |
751 | |
752 @item @samp{0} | |
753 Pad the field with zeros instead of spaces; the zeros are placed | |
754 after any sign. This flag is ignored if the @samp{-} flag is also | |
755 specified. | |
756 @end table | |
757 | |
758 The precision specifies how many digits follow the decimal-point | |
759 character for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. For | |
760 these conversions, the default precision is @code{6}. If the precision | |
761 is explicitly @code{0}, this suppresses the decimal point character | |
762 entirely. For the @samp{%g} and @samp{%G} conversions, the precision | |
763 specifies how many significant digits to print. Significant digits are | |
764 the first digit before the decimal point, and all the digits after it. | |
765 If the precision is @code{0} or not specified for @samp{%g} or | |
766 @samp{%G}, it is treated like a value of @code{1}. If the value being | |
767 printed cannot be expressed precisely in the specified number of digits, | |
768 the value is rounded to the nearest number that fits. | |
769 | |
4167 | 770 @node Other Output Conversions |
3294 | 771 @subsection Other Output Conversions |
772 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
773 This section describes miscellaneous conversions for @code{printf}. |
3294 | 774 |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
775 The @samp{%c} conversion prints a single character. The @samp{-} |
3294 | 776 flag can be used to specify left-justification in the field, but no |
777 other flags are defined, and no precision or type modifier can be given. | |
778 For example: | |
779 | |
6670 | 780 @example |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
781 printf ("%c%c%c%c%c", "h", "e", "l", "l", "o"); |
6670 | 782 @end example |
3294 | 783 |
784 @noindent | |
785 prints @samp{hello}. | |
786 | |
787 The @samp{%s} conversion prints a string. The corresponding argument | |
788 must be a string. A precision can be specified to indicate the maximum | |
789 number of characters to write; otherwise characters in the string up to | |
790 but not including the terminating null character are written to the | |
791 output stream. The @samp{-} flag can be used to specify | |
792 left-justification in the field, but no other flags or type modifiers | |
793 are defined for this conversion. For example: | |
794 | |
6670 | 795 @example |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
796 printf ("%3s%-6s", "no", "where"); |
6670 | 797 @end example |
3294 | 798 |
799 @noindent | |
800 prints @samp{ nowhere } (note the leading and trailing spaces). | |
801 | |
4167 | 802 @node Formatted Input |
3294 | 803 @subsection Formatted Input |
804 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
805 Octave provides the @code{scanf}, @code{fscanf}, and @code{sscanf} |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
806 functions to read formatted input. There are two forms of each of these |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
807 functions. One can be used to extract vectors of data from a file, and |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
808 the other is more `C-like'. |
3294 | 809 |
3428 | 810 @DOCSTRING(fscanf) |
3294 | 811 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
812 @DOCSTRING(scanf) |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
813 |
3372 | 814 @DOCSTRING(sscanf) |
3294 | 815 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
816 Calls to @code{scanf} are superficially similar to calls to |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
817 @code{printf} in that arbitrary arguments are read under the control of |
3294 | 818 a template string. While the syntax of the conversion specifications in |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
819 the template is very similar to that for @code{printf}, the |
3294 | 820 interpretation of the template is oriented more towards free-format |
821 input and simple pattern matching, rather than fixed-field formatting. | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
822 For example, most @code{scanf} conversions skip over any amount of |
3294 | 823 ``white space'' (including spaces, tabs, and newlines) in the input |
824 file, and there is no concept of precision for the numeric input | |
825 conversions as there is for the corresponding output conversions. | |
826 Ordinarily, non-whitespace characters in the template are expected to | |
27796
e7dd7d98a058
doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents:
27763
diff
changeset
|
827 match characters in the input stream exactly. For example, note that |
27384
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
828 @code{sscanf} parses the string and whitespace differently when using |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
829 mixed numeric and string output types: |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
830 @cindex conversion specifications (@code{scanf}) |
3294 | 831 |
27384
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
832 @example |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
833 @group |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
834 teststr = "1 is a lonely number"; |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
835 sscanf (teststr, "%s is a %s") |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
836 @result{} 1lonelynumber |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
837 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
838 sscanf (teststr, "%g is a %s") |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
839 @result{} |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
840 1 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
841 108 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
842 111 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
843 110 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
844 101 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
845 108 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
846 121 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
847 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
848 [a, b, c] = sscanf ("1 is a lonely number", "%g is a %s %s", "C") |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
849 @result{} a = 1 |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
850 @result{} b = lonely |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
851 @result{} c = number |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
852 @end group |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
853 @end example |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
854 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
855 When a @dfn{matching failure} occurs, @code{scanf} returns immediately, |
3294 | 856 leaving the first non-matching character as the next character to be |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
857 read from the stream, and @code{scanf} returns all the items that were |
3294 | 858 successfully converted. |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
859 @cindex matching failure, in @code{scanf} |
3294 | 860 |
861 The formatted input functions are not used as frequently as the | |
862 formatted output functions. Partly, this is because it takes some care | |
863 to use them properly. Another reason is that it is difficult to recover | |
864 from a matching error. | |
865 | |
27384
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
866 The specific flags and modifiers that are permitted in the template string |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
867 and their interpretation are all described in more detail in the following |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
868 sections. |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
869 |
4167 | 870 @node Input Conversion Syntax |
3294 | 871 @subsection Input Conversion Syntax |
872 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
873 A @code{scanf} template string is a string that contains ordinary |
3294 | 874 multibyte characters interspersed with conversion specifications that |
875 start with @samp{%}. | |
876 | |
877 Any whitespace character in the template causes any number of whitespace | |
878 characters in the input stream to be read and discarded. The whitespace | |
879 characters that are matched need not be exactly the same whitespace | |
880 characters that appear in the template string. For example, write | |
881 @samp{ , } in the template to recognize a comma with optional whitespace | |
882 before and after. | |
883 | |
884 Other characters in the template string that are not part of conversion | |
885 specifications must match characters in the input stream exactly; if | |
886 this is not the case, a matching failure occurs. | |
887 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
888 The conversion specifications in a @code{scanf} template string |
3294 | 889 have the general form: |
890 | |
6670 | 891 @example |
3294 | 892 % @var{flags} @var{width} @var{type} @var{conversion} |
6670 | 893 @end example |
3294 | 894 |
895 In more detail, an input conversion specification consists of an initial | |
896 @samp{%} character followed in sequence by: | |
897 | |
898 @itemize @bullet | |
899 @item | |
900 An optional @dfn{flag character} @samp{*}, which says to ignore the text | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
901 read for this specification. When @code{scanf} finds a conversion |
3294 | 902 specification that uses this flag, it reads input as directed by the |
903 rest of the conversion specification, but it discards this input, does | |
904 not return any value, and does not increment the count of | |
905 successful assignments. | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
906 @cindex flag character (@code{scanf}) |
3294 | 907 |
908 @item | |
909 An optional decimal integer that specifies the @dfn{maximum field | |
910 width}. Reading of characters from the input stream stops either when | |
911 this maximum is reached or when a non-matching character is found, | |
912 whichever happens first. Most conversions discard initial whitespace | |
913 characters, and these discarded characters don't count towards the | |
914 maximum field width. Conversions that do not discard initial whitespace | |
915 are explicitly documented. | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
916 @cindex maximum field width (@code{scanf}) |
3294 | 917 |
918 @item | |
919 An optional type modifier character. This character is ignored by | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
920 Octave's @code{scanf} function, but is recognized to provide |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
921 compatibility with the C language @code{scanf}. |
3294 | 922 |
923 @item | |
924 A character that specifies the conversion to be applied. | |
925 @end itemize | |
926 | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
927 The exact options that are permitted and how they are interpreted vary |
3294 | 928 between the different conversion specifiers. See the descriptions of the |
27384
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
929 individual conversions in @ref{Table of Input Conversions} for |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
930 information about the particular options that they allow. |
3294 | 931 |
4167 | 932 @node Table of Input Conversions |
3294 | 933 @subsection Table of Input Conversions |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
934 @cindex input conversions, for @code{scanf} |
3294 | 935 |
936 Here is a table that summarizes the various conversion specifications: | |
937 | |
938 @table @asis | |
939 @item @samp{%d} | |
940 Matches an optionally signed integer written in decimal. @xref{Numeric | |
941 Input Conversions}. | |
942 | |
943 @item @samp{%i} | |
944 Matches an optionally signed integer in any of the formats that the C | |
945 language defines for specifying an integer constant. @xref{Numeric | |
946 Input Conversions}. | |
947 | |
948 @item @samp{%o} | |
949 Matches an unsigned integer written in octal radix. | |
950 @xref{Numeric Input Conversions}. | |
951 | |
952 @item @samp{%u} | |
953 Matches an unsigned integer written in decimal radix. | |
954 @xref{Numeric Input Conversions}. | |
955 | |
956 @item @samp{%x}, @samp{%X} | |
957 Matches an unsigned integer written in hexadecimal radix. | |
958 @xref{Numeric Input Conversions}. | |
959 | |
960 @item @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, @samp{%G} | |
961 Matches an optionally signed floating-point number. @xref{Numeric Input | |
962 Conversions}. | |
963 | |
964 @item @samp{%s} | |
965 Matches a string containing only non-whitespace characters. | |
966 @xref{String Input Conversions}. | |
967 | |
968 @item @samp{%c} | |
969 Matches a string of one or more characters; the number of characters | |
970 read is controlled by the maximum field width given for the conversion. | |
971 @xref{String Input Conversions}. | |
972 | |
973 @item @samp{%%} | |
974 This matches a literal @samp{%} character in the input stream. No | |
975 corresponding argument is used. | |
976 @end table | |
977 | |
978 If the syntax of a conversion specification is invalid, the behavior is | |
979 undefined. If there aren't enough function arguments provided to supply | |
980 addresses for all the conversion specifications in the template strings | |
981 that perform assignments, or if the arguments are not of the correct | |
982 types, the behavior is also undefined. On the other hand, extra | |
983 arguments are simply ignored. | |
984 | |
4167 | 985 @node Numeric Input Conversions |
3294 | 986 @subsection Numeric Input Conversions |
987 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
988 This section describes the @code{scanf} conversions for reading numeric |
3294 | 989 values. |
990 | |
991 The @samp{%d} conversion matches an optionally signed integer in decimal | |
992 radix. | |
993 | |
994 The @samp{%i} conversion matches an optionally signed integer in any of | |
995 the formats that the C language defines for specifying an integer | |
996 constant. | |
997 | |
998 For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012} | |
999 could be read in as integers under the @samp{%i} conversion. Each of | |
1000 these specifies a number with decimal value @code{10}. | |
1001 | |
1002 The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned | |
1003 integers in octal, decimal, and hexadecimal radices, respectively. | |
1004 | |
1005 The @samp{%X} conversion is identical to the @samp{%x} conversion. They | |
1006 both permit either uppercase or lowercase letters to be used as digits. | |
1007 | |
23966
b833c7334308
* io.txi: Update note about scanf 'h' and 'l' modifiers.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
1008 By default, integers are read as 32-bit quantities. With the @samp{h} |
b833c7334308
* io.txi: Update note about scanf 'h' and 'l' modifiers.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
1009 modifier, 16-bit integers are used, and with the @samp{l} modifier, |
b833c7334308
* io.txi: Update note about scanf 'h' and 'l' modifiers.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
1010 64-bit integers are used. |
3294 | 1011 |
27384
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
1012 The @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, and @samp{%G} conversions |
27796
e7dd7d98a058
doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents:
27763
diff
changeset
|
1013 match optionally signed floating-point numbers. All five conversion |
27384
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
1014 specifications behave identically, and will read in numerical values of |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
1015 any floating point display style. |
918a3df652c3
doc: Update formatted input, add missing info on float conversions (bug #56557)
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
26376
diff
changeset
|
1016 |
4167 | 1017 @node String Input Conversions |
3294 | 1018 @subsection String Input Conversions |
1019 | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
1020 This section describes the @code{scanf} input conversions for reading |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1021 string and character values: @samp{%s} and @samp{%c}. |
3294 | 1022 |
1023 The @samp{%c} conversion is the simplest: it matches a fixed number of | |
1024 characters, always. The maximum field with says how many characters to | |
1025 read; if you don't specify the maximum, the default is 1. This | |
1026 conversion does not skip over initial whitespace characters. It reads | |
1027 precisely the next @var{n} characters, and fails if it cannot get that | |
1028 many. | |
1029 | |
1030 The @samp{%s} conversion matches a string of non-whitespace characters. | |
1031 It skips and discards initial whitespace, but stops when it encounters | |
1032 more whitespace after having read something. | |
1033 | |
1034 For example, reading the input: | |
1035 | |
6670 | 1036 @example |
3294 | 1037 hello, world |
6670 | 1038 @end example |
3294 | 1039 |
1040 @noindent | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
1041 with the conversion @samp{%10c} produces @qcode{" hello, wo"}, but |
3294 | 1042 reading the same input with the conversion @samp{%10s} produces |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
1043 @qcode{"hello,"}. |
3294 | 1044 |
4167 | 1045 @node Binary I/O |
3294 | 1046 @subsection Binary I/O |
1047 | |
1048 Octave can read and write binary data using the functions @code{fread} | |
1049 and @code{fwrite}, which are patterned after the standard C functions | |
6939 | 1050 with the same names. They are able to automatically swap the byte order |
1051 of integer data and convert among the supported floating point formats | |
3294 | 1052 as the data are read. |
1053 | |
3372 | 1054 @DOCSTRING(fread) |
3294 | 1055 |
3372 | 1056 @DOCSTRING(fwrite) |
3294 | 1057 |
4167 | 1058 @node Temporary Files |
3294 | 1059 @subsection Temporary Files |
1060 | |
6666 | 1061 Sometimes one needs to write data to a file that is only temporary. |
1062 This is most commonly used when an external program launched from | |
1063 within Octave needs to access data. When Octave exits all temporary | |
1064 files will be deleted, so this step need not be executed manually. | |
1065 | |
4328 | 1066 @DOCSTRING(mkstemp) |
1067 | |
1068 @DOCSTRING(tmpfile) | |
1069 | |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1070 @DOCSTRING(tempname) |
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1071 |
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1072 @DOCSTRING(tempdir) |
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1073 |
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1074 @DOCSTRING(P_tmpdir) |
3294 | 1075 |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16875
diff
changeset
|
1076 @node EOF and Errors |
3294 | 1077 @subsection End of File and Errors |
1078 | |
6666 | 1079 Once a file has been opened its status can be acquired. As an example |
1080 the @code{feof} functions determines if the end of the file has been | |
1081 reached. This can be very useful when reading small parts of a file | |
1082 at a time. The following example shows how to read one line at a time | |
1083 from a file until the end has been reached. | |
1084 | |
1085 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1086 @group |
6666 | 1087 filename = "myfile.txt"; |
1088 fid = fopen (filename, "r"); | |
1089 while (! feof (fid) ) | |
1090 text_line = fgetl (fid); | |
1091 endwhile | |
1092 fclose (fid); | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1093 @end group |
6666 | 1094 @end example |
1095 | |
1096 @noindent | |
1097 Note that in some situations it is more efficient to read the entire | |
1098 contents of a file and then process it, than it is to read it line by | |
1099 line. This has the potential advantage of removing the loop in the | |
1100 above code. | |
1101 | |
3372 | 1102 @DOCSTRING(feof) |
3294 | 1103 |
3372 | 1104 @DOCSTRING(ferror) |
3294 | 1105 |
8817
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8569
diff
changeset
|
1106 @DOCSTRING(fclear) |
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8569
diff
changeset
|
1107 |
3372 | 1108 @DOCSTRING(freport) |
3294 | 1109 |
4167 | 1110 @node File Positioning |
3294 | 1111 @subsection File Positioning |
1112 | |
1113 Three functions are available for setting and determining the position of | |
1114 the file pointer for a given file. | |
1115 | |
3372 | 1116 @DOCSTRING(ftell) |
1117 | |
1118 @DOCSTRING(fseek) | |
3294 | 1119 |
3372 | 1120 @DOCSTRING(SEEK_SET) |
3294 | 1121 |
25005
db1e3f3664f4
doc: Update documentation for SEEK_SET, SEEK_CUR, SEEK_END.
Rik <rik@octave.org>
parents:
24801
diff
changeset
|
1122 @DOCSTRING(SEEK_CUR) |
db1e3f3664f4
doc: Update documentation for SEEK_SET, SEEK_CUR, SEEK_END.
Rik <rik@octave.org>
parents:
24801
diff
changeset
|
1123 |
db1e3f3664f4
doc: Update documentation for SEEK_SET, SEEK_CUR, SEEK_END.
Rik <rik@octave.org>
parents:
24801
diff
changeset
|
1124 @DOCSTRING(SEEK_END) |
db1e3f3664f4
doc: Update documentation for SEEK_SET, SEEK_CUR, SEEK_END.
Rik <rik@octave.org>
parents:
24801
diff
changeset
|
1125 |
3372 | 1126 @DOCSTRING(frewind) |
3294 | 1127 |
1128 The following example stores the current file position in the variable | |
1129 @code{marker}, moves the pointer to the beginning of the file, reads | |
1130 four characters, and then returns to the original position. | |
1131 | |
1132 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1133 @group |
3294 | 1134 marker = ftell (myfile); |
1135 frewind (myfile); | |
1136 fourch = fgets (myfile, 4); | |
1137 fseek (myfile, marker, SEEK_SET); | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1138 @end group |
3294 | 1139 @end example |