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