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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
2 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
3 @c This file is part of Octave.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
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
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
15 @c You should have received a copy of the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
18
4169
d5667eaf8859 [project @ 2002-11-12 03:13:42 by jwe]
jwe
parents: 4167
diff changeset
19 @node Input and Output
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
20 @chapter Input and Output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
21
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
22 Octave supports several ways of reading and writing data to or from the
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
26 after the C standard library are also provided by Octave.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
27
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
31 @end menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
32
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
33 @node Basic Input and Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
34 @section Basic Input and Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
35
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
36 @c We could use a two-line introduction here...
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
37
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
42 @end menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
43
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
44 @node Terminal Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
45 @subsection Terminal Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
46
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
47 Since Octave normally prints the value of an expression as soon as it
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
48 has been evaluated, the simplest of all I/O functions is a simple
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
49 expression. For example, the following expression will display the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
50 value of @samp{pi}
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
51
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
57 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
58
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
59 This works well as long as it is acceptable to have the name of the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
60 variable (or @samp{ans}) printed along with the value. To print the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
61 value of a variable without printing its name, use the function
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
62 @code{disp}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
63
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
64 The @code{format} command offers some control over the way Octave prints
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
65 values with @code{disp} and through the normal echoing mechanism.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
66
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
67 @DOCSTRING(disp)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
73 @DOCSTRING(format)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
74
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
75 @menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
76 * Paging Screen Output::
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
77 @end menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
78
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
79 @node Paging Screen Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
80 @subsubsection Paging Screen Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
100
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
101 @DOCSTRING(more)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
102
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
103 @DOCSTRING(PAGER)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
104
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 5775
diff changeset
105 @DOCSTRING(PAGER_FLAGS)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 5775
diff changeset
106
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
107 @DOCSTRING(page_screen_output)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
108
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
109 @DOCSTRING(page_output_immediately)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
110
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
111 @DOCSTRING(fflush)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
114 @c
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
115 @c warning: connection to external pager (pid = 9334) lost --
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
116 @c warning: pending computations and output may be lost
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
117 @c warning: broken pipe
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
118
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
119 @node Terminal Input
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
120 @subsection Terminal Input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
121
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
122 Octave has three functions that make it easy to prompt users for
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
123 input. The @code{input} and @code{menu} functions are normally
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
124 used for managing an interactive dialog with a user, and the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
125 @code{keyboard} function is normally used for doing simple debugging.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
126
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
127 @DOCSTRING(input)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
128
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
129 @DOCSTRING(menu)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bd0a70c3f2db [project @ 2007-05-22 02:27:43 by jwe]
jwe
parents: 6556
diff changeset
133 For @code{input}, the normal command line history and editing functions
bd0a70c3f2db [project @ 2007-05-22 02:27:43 by jwe]
jwe
parents: 6556
diff changeset
134 are available at the prompt.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
135
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
136 Octave also has a function that makes it possible to get a single
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
137 character from the keyboard without requiring the user to type a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
138 carriage return.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
139
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
140 @DOCSTRING(kbhit)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
141
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
142 @node Simple File I/O
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
143 @subsection Simple File I/O
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
144
5225
6434e63553f0 [project @ 2005-03-17 17:53:01 by jwe]
jwe
parents: 5213
diff changeset
145 @cindex saving data
6434e63553f0 [project @ 2005-03-17 17:53:01 by jwe]
jwe
parents: 5213
diff changeset
146 @cindex loading data
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
147 The @code{save} and @code{load} commands allow data to be written to and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
148 read from disk files in various formats. The default format of files
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
151
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
152 As an example the following code creates a 3-by-3 matrix and saves it
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
153 to the file @samp{myfile.mat}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
154
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
157 A = [ 1:3; 4:6; 7:9 ];
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
160 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
161
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
162 Once one or more variables have been saved to a file, they can be
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
163 read into memory using the @code{load} command.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
164
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
167 load myfile.mat
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
168 A
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
171 @print{} 1 2 3
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
172 @print{} 4 5 6
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
175 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
176
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
177 @DOCSTRING(save)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
178
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
179 There are three functions that modify the behavior of @code{save}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
182
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
183 @DOCSTRING(save_precision)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
184
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
185 @DOCSTRING(save_header_format_string)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
191 @DOCSTRING(native_float_format)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
192
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
193 It is possible to write data to a file in a similar way to the
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
194 @code{disp} function for writing data to the screen. The @code{fdisp}
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
195 works just like @code{disp} except its first argument is a file pointer
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
196 as created by @code{fopen}. As an example, the following code writes
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
197 to data @samp{myfile.txt}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
198
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
201 fid = fopen ("myfile.txt", "w");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
202 fdisp (fid, "3/8 is ");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
203 fdisp (fid, 3/8);
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
206 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
207
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
208 @noindent
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
209 @xref{Opening and Closing Files}, for details on how to use @code{fopen}
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
210 and @code{fclose}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
211
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
212 @DOCSTRING(fdisp)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
c9ec943ebe65 doc fix
Ben Abbott <bpabbott@mac.com>
parents: 11147
diff changeset
225 Formatted data from can be read from, or written to, text files as well.
c9ec943ebe65 doc fix
Ben Abbott <bpabbott@mac.com>
parents: 11147
diff changeset
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
4e6249815ea6 rescale.m: more fixes (bug #56639).
Rik <rik@octave.org>
parents: 27756
diff changeset
236 After importing, the data may need to be transformed before further analysis.
4e6249815ea6 rescale.m: more fixes (bug #56639).
Rik <rik@octave.org>
parents: 27756
diff changeset
237 The @code{rescale} function can shift and normalize a data set to a specified
4e6249815ea6 rescale.m: more fixes (bug #56639).
Rik <rik@octave.org>
parents: 27756
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
242 @menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
243 * Saving Data on Unexpected Exits::
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
244 @end menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
245
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
246 @node Saving Data on Unexpected Exits
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
247 @subsubsection Saving Data on Unexpected Exits
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
253 most often is reasonable it can be changed through the following
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
254 functions.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
255
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
256 @DOCSTRING(crash_dumps_octave_core)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
257
4449
e2c91da0aa16 [project @ 2003-07-09 02:21:11 by jwe]
jwe
parents: 4328
diff changeset
258 @DOCSTRING(sighup_dumps_octave_core)
e2c91da0aa16 [project @ 2003-07-09 02:21:11 by jwe]
jwe
parents: 4328
diff changeset
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
e2c91da0aa16 [project @ 2003-07-09 02:21:11 by jwe]
jwe
parents: 4328
diff changeset
262 @DOCSTRING(sigterm_dumps_octave_core)
e2c91da0aa16 [project @ 2003-07-09 02:21:11 by jwe]
jwe
parents: 4328
diff changeset
263
5287
f14bdd2bec91 [project @ 2005-04-19 15:15:21 by jwe]
jwe
parents: 5225
diff changeset
264 @DOCSTRING(octave_core_file_options)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
265
6550
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
266 @DOCSTRING(octave_core_file_limit)
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
267
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
268 @DOCSTRING(octave_core_file_name)
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
269
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
270 @node C-Style I/O Functions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
271 @section C-Style I/O Functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
272
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
273 Octave's C-style input and output functions provide most of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
274 functionality of the C programming language's standard I/O library. The
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
275 argument lists for some of the input functions are slightly different,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
276 however, because Octave has no way of passing arguments by reference.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
279 to an integer file number, as returned by @code{fopen}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
280
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
281 There are three files that are always available. Although these files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
282 can be accessed using their corresponding numeric file ids, you should
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
283 always use the symbolic names given in the table below, since it will
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
284 make your programs easier to understand.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
285
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
286 @DOCSTRING(stdin)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
287
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
288 @DOCSTRING(stdout)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
289
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
290 @DOCSTRING(stderr)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
291
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
312 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
313
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
314 @node Opening and Closing Files
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
315 @subsection Opening and Closing Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
316
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
317 When reading data from a file it must be opened for reading first, and
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
318 likewise when writing to a file. The @code{fopen} function returns a
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
319 pointer to an open file that is ready to be read or written. Once all
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
320 data has been read from or written to the opened file it should be closed.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
321 The @code{fclose} function does this. The following code illustrates
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
322 the basic pattern for writing to a file, but a very similar pattern is
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
323 used when reading a file.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
324
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
327 filename = "myfile.txt";
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
332 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
333
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
334 @DOCSTRING(fopen)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
335
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
336 @DOCSTRING(fclose)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
340 @node Simple Output
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
341 @subsection Simple Output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
356
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
357 @DOCSTRING(fputs)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
365 @node Line-Oriented Input
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
366 @subsection Line-Oriented Input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
367
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
368 To read from a file it must be opened for reading using @code{fopen}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
369 Then a line can be read from the file using @code{fgetl} as the following
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
370 code illustrates
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
371
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
374 fid = fopen ("free.txt");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
375 txt = fgetl (fid)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
376 @print{} Free Software is needed for Free Science
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
379 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
380
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
381 @noindent
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
382 This of course assumes that the file @samp{free.txt} exists and contains
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
383 the line @samp{Free Software is needed for Free Science}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
384
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
385 @DOCSTRING(fgetl)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
386
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
387 @DOCSTRING(fgets)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
388
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
389 @DOCSTRING(fskipl)
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
390
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
391 @node Formatted Output
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
392 @subsection Formatted Output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
398 interpret the format template differently in order to improve the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
399 performance of printing vector and matrix values.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
407 @DOCSTRING(fprintf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
408
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
409 @DOCSTRING(sprintf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
413 information not only about the number of additional arguments, but also
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
414 about their types and what style should be used for printing them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
415
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
416 Ordinary characters in the template string are simply written to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
417 output stream as-is, while @dfn{conversion specifications} introduced by
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
418 a @samp{%} character in the template cause subsequent arguments to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
421
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
424 pct = 37;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
429 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
430
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
431 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
432 produces output like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
433
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
439 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
440
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
441 This example shows the use of the @samp{%d} conversion to specify that a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
442 scalar argument should be printed in decimal notation, the @samp{%s}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
443 conversion to specify printing of a string argument, and the @samp{%%}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
444 conversion to print a literal @samp{%} character.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
445
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
446 There are also conversions for printing an integer argument as an
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
447 unsigned value in octal, decimal, or hexadecimal radix (@samp{%o},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
448 @samp{%u}, or @samp{%x}, respectively); or as a character value
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
449 (@samp{%c}).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
450
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
451 Floating-point numbers can be printed in normal, fixed-point notation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
452 using the @samp{%f} conversion or in exponential notation using the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
453 @samp{%e} conversion. The @samp{%g} conversion uses either @samp{%e}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
454 or @samp{%f} format, depending on what is more appropriate for the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
455 magnitude of the particular number.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
456
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
457 You can control formatting more precisely by writing @dfn{modifiers}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
458 between the @samp{%} and the character that indicates which conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
459 to apply. These slightly alter the ordinary behavior of the conversion.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
460 For example, most conversion specifications permit you to specify a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
461 minimum field width and a flag indicating whether you want the result
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
462 left- or right-justified within the field.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
463
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
464 The specific flags and modifiers that are permitted and their
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
465 interpretation vary depending on the particular conversion. They're all
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
466 described in more detail in the following sections.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
467
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
468 @node Output Conversion for Matrices
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
469 @subsection Output Conversion for Matrices
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
470
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
471 When given a matrix value, Octave's formatted output functions cycle
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
474
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
475 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
478
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
479 @print{} 1.00 5.00e-01 0.3333
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
480 @print{} 0.50 3.33e-01 0.25
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
481 @print{} 0.33 2.50e-01 0.2
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
482 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
483 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
484
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
485 If more than one value is to be printed in a single call, the output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
486 functions do not return to the beginning of the format template when
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
487 moving on from one value to the next. This can lead to confusing output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
490
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
491 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
494
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
495 @print{} 1.00 2.00e+00 3
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
496 @print{} 4.00
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
497 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
498 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
499
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
500 If this is not what you want, use a series of calls instead of just one.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
501
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
502 @node Output Conversion Syntax
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
503 @subsection Output Conversion Syntax
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
504
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
508
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
509 Characters in the template string that are not part of a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
510 conversion specification are printed as-is to the output stream.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
514
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
515 @example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
516 % @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion}
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
517 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
518
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
519 For example, in the conversion specifier @samp{%-10.8ld}, the @samp{-}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
520 is a flag, @samp{10} specifies the field width, the precision is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
521 @samp{8}, the letter @samp{l} is a type modifier, and @samp{d} specifies
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
522 the conversion style. (This particular type specifier says to print a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
523 numeric argument in decimal notation, with a minimum of 8 digits
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
524 left-justified in a field at least 10 characters wide.)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
525
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
526 In more detail, output conversion specifications consist of an
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
527 initial @samp{%} character followed in sequence by:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
528
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
531 Zero or more @dfn{flag characters} that modify the normal behavior of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
534
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
535 @item
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
536 An optional decimal integer specifying the @dfn{minimum field width}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
537 If the normal conversion produces fewer characters than this, the field
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
538 is padded with spaces to the specified width. This is a @emph{minimum}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
539 value; if the normal conversion produces more characters than this, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
540 field is @emph{not} truncated. Normally, the output is right-justified
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
543
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
544 You can also specify a field width of @samp{*}. This means that the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
545 next argument in the argument list (before the actual value to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
546 printed) is used as the field width. The value is rounded to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
547 nearest integer. If the value is negative, this means to set the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
548 @samp{-} flag (see below) and to use the absolute value as the field
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
549 width.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
550
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
551 @item
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
552 An optional @dfn{precision} to specify the number of digits to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
553 written for the numeric conversions. If the precision is specified, it
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
554 consists of a period (@samp{.}) followed optionally by a decimal integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
557
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
558 You can also specify a precision of @samp{*}. This means that the next
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
559 argument in the argument list (before the actual value to be printed) is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
560 used as the precision. The value must be an integer, and is ignored
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
561 if it is negative.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
562
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
563 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
567
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
568 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
569 A character that specifies the conversion to be applied.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
570 @end itemize
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
573 between the different conversion specifiers. See the descriptions of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
574 individual conversions for information about the particular options that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
575 they use.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
576
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
577 @node Table of Output Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
580
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
581 Here is a table summarizing what all the different conversions do:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
582
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
583 @table @asis
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
584 @item @samp{%d}, @samp{%i}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
585 Print an integer as a signed decimal number. @xref{Integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
588 (@pxref{Table of Input Conversions}).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
589
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
590 @item @samp{%o}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
591 Print an integer as an unsigned octal number. @xref{Integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
592 Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
593
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
594 @item @samp{%u}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
595 Print an integer as an unsigned decimal number. @xref{Integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
596 Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
597
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
598 @item @samp{%x}, @samp{%X}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
601 Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
602
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
603 @item @samp{%f}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
604 Print a floating-point number in normal (fixed-point) notation.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
605 @xref{Floating-Point Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
606
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
607 @item @samp{%e}, @samp{%E}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
610 Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
611
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
612 @item @samp{%g}, @samp{%G}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
613 Print a floating-point number in either normal (fixed-point) or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
616 @xref{Floating-Point Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
617
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
618 @item @samp{%c}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
619 Print a single character. @xref{Other Output Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
620
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
621 @item @samp{%s}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
622 Print a string. @xref{Other Output Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
623
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
624 @item @samp{%%}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
625 Print a literal @samp{%} character. @xref{Other Output Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
626 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
627
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
649 arguments than conversion specifications, the extra argument values are
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
650 simply ignored; this is sometimes useful.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
651
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
652 @node Integer Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
653 @subsection Integer Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
654
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
655 This section describes the options for the @samp{%d}, @samp{%i},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
656 @samp{%o}, @samp{%u}, @samp{%x}, and @samp{%X} conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
657 specifications. These conversions print integers in various formats.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
658
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
659 The @samp{%d} and @samp{%i} conversion specifications both print an
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
660 numeric argument as a signed decimal number; while @samp{%o},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
661 @samp{%u}, and @samp{%x} print the argument as an unsigned octal,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
662 decimal, or hexadecimal number (respectively). The @samp{%X} conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
663 specification is just like @samp{%x} except that it uses the characters
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
664 @samp{ABCDEF} as digits instead of @samp{abcdef}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
665
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
666 The following flags are meaningful:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
667
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
668 @table @asis
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
669 @item @samp{-}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
670 Left-justify the result in the field (instead of the normal
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
671 right-justification).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
672
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
673 @item @samp{+}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
674 For the signed @samp{%d} and @samp{%i} conversions, print a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
675 plus sign if the value is positive.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
676
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
677 @item @samp{ }
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
678 For the signed @samp{%d} and @samp{%i} conversions, if the result
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
679 doesn't start with a plus or minus sign, prefix it with a space
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
680 character instead. Since the @samp{+} flag ensures that the result
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
681 includes a sign, this flag is ignored if you supply both of them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
682
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
683 @item @samp{#}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
684 For the @samp{%o} conversion, this forces the leading digit to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
685 @samp{0}, as if by increasing the precision. For @samp{%x} or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
686 @samp{%X}, this prefixes a leading @samp{0x} or @samp{0X} (respectively)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
687 to the result. This doesn't do anything useful for the @samp{%d},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
688 @samp{%i}, or @samp{%u} conversions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
689
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
690 @item @samp{0}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
691 Pad the field with zeros instead of spaces. The zeros are placed after
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
692 any indication of sign or base. This flag is ignored if the @samp{-}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
693 flag is also specified, or if a precision is specified.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
694 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
695
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
696 If a precision is supplied, it specifies the minimum number of digits to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
697 appear; leading zeros are produced if necessary. If you don't specify a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
698 precision, the number is printed with as many digits as it needs. If
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
699 you convert a value of zero with an explicit precision of zero, then no
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
700 characters at all are produced.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
701
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
702 @node Floating-Point Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
703 @subsection Floating-Point Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
704
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
705 This section discusses the conversion specifications for floating-point
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
706 numbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
707 conversions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
708
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
709 The @samp{%f} conversion prints its argument in fixed-point notation,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
710 producing output of the form
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
711 @w{[@code{-}]@var{ddd}@code{.}@var{ddd}},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
712 where the number of digits following the decimal point is controlled
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
713 by the precision you specify.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
714
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
715 The @samp{%e} conversion prints its argument in exponential notation,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
716 producing output of the form
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
717 @w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
718 Again, the number of digits following the decimal point is controlled by
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
719 the precision. The exponent always contains at least two digits. The
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
720 @samp{%E} conversion is similar but the exponent is marked with the letter
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
721 @samp{E} instead of @samp{e}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
722
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
723 The @samp{%g} and @samp{%G} conversions print the argument in the style
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
724 of @samp{%e} or @samp{%E} (respectively) if the exponent would be less
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
725 than -4 or greater than or equal to the precision; otherwise they use the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
726 @samp{%f} style. Trailing zeros are removed from the fractional portion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
727 of the result and a decimal-point character appears only if it is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
728 followed by a digit.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
729
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
732
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
733 @table @asis
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
734 @item @samp{-}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
735 Left-justify the result in the field. Normally the result is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
736 right-justified.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
737
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
738 @item @samp{+}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
739 Always include a plus or minus sign in the result.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
740
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
741 @item @samp{ }
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
742 If the result doesn't start with a plus or minus sign, prefix it with a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
743 space instead. Since the @samp{+} flag ensures that the result includes
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
744 a sign, this flag is ignored if you supply both of them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
745
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
746 @item @samp{#}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
747 Specifies that the result should always include a decimal point, even
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
748 if no digits follow it. For the @samp{%g} and @samp{%G} conversions,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
749 this also forces trailing zeros after the decimal point to be left
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
750 in place where they would otherwise be removed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
751
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
752 @item @samp{0}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
753 Pad the field with zeros instead of spaces; the zeros are placed
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
754 after any sign. This flag is ignored if the @samp{-} flag is also
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
755 specified.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
756 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
757
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
758 The precision specifies how many digits follow the decimal-point
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
759 character for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. For
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
760 these conversions, the default precision is @code{6}. If the precision
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
761 is explicitly @code{0}, this suppresses the decimal point character
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
762 entirely. For the @samp{%g} and @samp{%G} conversions, the precision
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
763 specifies how many significant digits to print. Significant digits are
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
764 the first digit before the decimal point, and all the digits after it.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
765 If the precision is @code{0} or not specified for @samp{%g} or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
766 @samp{%G}, it is treated like a value of @code{1}. If the value being
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
767 printed cannot be expressed precisely in the specified number of digits,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
768 the value is rounded to the nearest number that fits.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
769
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
770 @node Other Output Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
771 @subsection Other Output Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
776 flag can be used to specify left-justification in the field, but no
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
777 other flags are defined, and no precision or type modifier can be given.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
778 For example:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
779
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
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
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
782 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
783
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
784 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
785 prints @samp{hello}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
786
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
787 The @samp{%s} conversion prints a string. The corresponding argument
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
788 must be a string. A precision can be specified to indicate the maximum
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
789 number of characters to write; otherwise characters in the string up to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
790 but not including the terminating null character are written to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
791 output stream. The @samp{-} flag can be used to specify
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
792 left-justification in the field, but no other flags or type modifiers
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
793 are defined for this conversion. For example:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
794
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
795 @example
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
796 printf ("%3s%-6s", "no", "where");
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
797 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
798
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
799 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
800 prints @samp{ nowhere } (note the leading and trailing spaces).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
801
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
802 @node Formatted Input
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
803 @subsection Formatted Input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
809
3428
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3373
diff changeset
810 @DOCSTRING(fscanf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
814 @DOCSTRING(sscanf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
820 interpretation of the template is oriented more towards free-format
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
823 ``white space'' (including spaces, tabs, and newlines) in the input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
824 file, and there is no concept of precision for the numeric input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
825 conversions as there is for the corresponding output conversions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
860
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
861 The formatted input functions are not used as frequently as the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
862 formatted output functions. Partly, this is because it takes some care
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
863 to use them properly. Another reason is that it is difficult to recover
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
864 from a matching error.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
870 @node Input Conversion Syntax
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
871 @subsection Input Conversion Syntax
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
874 multibyte characters interspersed with conversion specifications that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
875 start with @samp{%}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
876
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
877 Any whitespace character in the template causes any number of whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
878 characters in the input stream to be read and discarded. The whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
879 characters that are matched need not be exactly the same whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
880 characters that appear in the template string. For example, write
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
881 @samp{ , } in the template to recognize a comma with optional whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
882 before and after.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
883
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
884 Other characters in the template string that are not part of conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
885 specifications must match characters in the input stream exactly; if
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
886 this is not the case, a matching failure occurs.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
889 have the general form:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
890
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
891 @example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
892 % @var{flags} @var{width} @var{type} @var{conversion}
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
893 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
894
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
895 In more detail, an input conversion specification consists of an initial
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
896 @samp{%} character followed in sequence by:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
897
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
898 @itemize @bullet
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
899 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
902 specification that uses this flag, it reads input as directed by the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
903 rest of the conversion specification, but it discards this input, does
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
904 not return any value, and does not increment the count of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
907
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
908 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
909 An optional decimal integer that specifies the @dfn{maximum field
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
910 width}. Reading of characters from the input stream stops either when
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
911 this maximum is reached or when a non-matching character is found,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
912 whichever happens first. Most conversions discard initial whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
913 characters, and these discarded characters don't count towards the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
914 maximum field width. Conversions that do not discard initial whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
917
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
918 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
922
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
923 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
924 A character that specifies the conversion to be applied.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
925 @end itemize
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
931
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
932 @node Table of Input Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
935
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
936 Here is a table that summarizes the various conversion specifications:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
937
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
938 @table @asis
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
939 @item @samp{%d}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
940 Matches an optionally signed integer written in decimal. @xref{Numeric
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
941 Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
942
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
943 @item @samp{%i}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
944 Matches an optionally signed integer in any of the formats that the C
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
945 language defines for specifying an integer constant. @xref{Numeric
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
946 Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
947
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
948 @item @samp{%o}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
949 Matches an unsigned integer written in octal radix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
950 @xref{Numeric Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
951
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
952 @item @samp{%u}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
953 Matches an unsigned integer written in decimal radix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
954 @xref{Numeric Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
955
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
956 @item @samp{%x}, @samp{%X}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
957 Matches an unsigned integer written in hexadecimal radix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
958 @xref{Numeric Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
959
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
960 @item @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, @samp{%G}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
961 Matches an optionally signed floating-point number. @xref{Numeric Input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
962 Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
963
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
964 @item @samp{%s}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
965 Matches a string containing only non-whitespace characters.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
966 @xref{String Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
967
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
968 @item @samp{%c}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
969 Matches a string of one or more characters; the number of characters
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
970 read is controlled by the maximum field width given for the conversion.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
971 @xref{String Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
972
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
973 @item @samp{%%}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
974 This matches a literal @samp{%} character in the input stream. No
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
975 corresponding argument is used.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
976 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
977
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
978 If the syntax of a conversion specification is invalid, the behavior is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
979 undefined. If there aren't enough function arguments provided to supply
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
980 addresses for all the conversion specifications in the template strings
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
981 that perform assignments, or if the arguments are not of the correct
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
982 types, the behavior is also undefined. On the other hand, extra
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
983 arguments are simply ignored.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
984
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
985 @node Numeric Input Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
986 @subsection Numeric Input Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
989 values.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
990
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
991 The @samp{%d} conversion matches an optionally signed integer in decimal
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
992 radix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
993
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
994 The @samp{%i} conversion matches an optionally signed integer in any of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
995 the formats that the C language defines for specifying an integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
996 constant.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
997
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
998 For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
999 could be read in as integers under the @samp{%i} conversion. Each of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1000 these specifies a number with decimal value @code{10}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1001
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1002 The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1003 integers in octal, decimal, and hexadecimal radices, respectively.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1004
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1005 The @samp{%X} conversion is identical to the @samp{%x} conversion. They
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1006 both permit either uppercase or lowercase letters to be used as digits.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
1017 @node String Input Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1018 @subsection String Input Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1022
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1023 The @samp{%c} conversion is the simplest: it matches a fixed number of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1024 characters, always. The maximum field with says how many characters to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1025 read; if you don't specify the maximum, the default is 1. This
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1026 conversion does not skip over initial whitespace characters. It reads
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1027 precisely the next @var{n} characters, and fails if it cannot get that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1028 many.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1029
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1030 The @samp{%s} conversion matches a string of non-whitespace characters.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1031 It skips and discards initial whitespace, but stops when it encounters
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1032 more whitespace after having read something.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1033
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1034 For example, reading the input:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1035
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
1036 @example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1037 hello, world
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
1038 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1039
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1044
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
1045 @node Binary I/O
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1046 @subsection Binary I/O
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1047
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1048 Octave can read and write binary data using the functions @code{fread}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1049 and @code{fwrite}, which are patterned after the standard C functions
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6788
diff changeset
1050 with the same names. They are able to automatically swap the byte order
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6788
diff changeset
1051 of integer data and convert among the supported floating point formats
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1052 as the data are read.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1053
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1054 @DOCSTRING(fread)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1055
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1056 @DOCSTRING(fwrite)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1057
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
1058 @node Temporary Files
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1059 @subsection Temporary Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1060
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1061 Sometimes one needs to write data to a file that is only temporary.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1062 This is most commonly used when an external program launched from
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1063 within Octave needs to access data. When Octave exits all temporary
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1064 files will be deleted, so this step need not be executed manually.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1065
4328
f7b63f362168 [project @ 2003-02-16 04:29:00 by jwe]
jwe
parents: 4169
diff changeset
1066 @DOCSTRING(mkstemp)
f7b63f362168 [project @ 2003-02-16 04:29:00 by jwe]
jwe
parents: 4169
diff changeset
1067
f7b63f362168 [project @ 2003-02-16 04:29:00 by jwe]
jwe
parents: 4169
diff changeset
1068 @DOCSTRING(tmpfile)
f7b63f362168 [project @ 2003-02-16 04:29:00 by jwe]
jwe
parents: 4169
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1077 @subsection End of File and Errors
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1078
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1079 Once a file has been opened its status can be acquired. As an example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1080 the @code{feof} functions determines if the end of the file has been
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1081 reached. This can be very useful when reading small parts of a file
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1082 at a time. The following example shows how to read one line at a time
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1083 from a file until the end has been reached.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1084
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1087 filename = "myfile.txt";
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1088 fid = fopen (filename, "r");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1089 while (! feof (fid) )
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1090 text_line = fgetl (fid);
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1091 endwhile
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
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
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1094 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1095
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1096 @noindent
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1097 Note that in some situations it is more efficient to read the entire
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1098 contents of a file and then process it, than it is to read it line by
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1099 line. This has the potential advantage of removing the loop in the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1100 above code.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1101
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1102 @DOCSTRING(feof)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1103
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1104 @DOCSTRING(ferror)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1108 @DOCSTRING(freport)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1109
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
1110 @node File Positioning
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1111 @subsection File Positioning
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1112
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1113 Three functions are available for setting and determining the position of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1114 the file pointer for a given file.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1115
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1116 @DOCSTRING(ftell)
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1117
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1118 @DOCSTRING(fseek)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1119
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1120 @DOCSTRING(SEEK_SET)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1126 @DOCSTRING(frewind)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1127
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1128 The following example stores the current file position in the variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1129 @code{marker}, moves the pointer to the beginning of the file, reads
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1130 four characters, and then returns to the original position.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1131
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1134 marker = ftell (myfile);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1135 frewind (myfile);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1136 fourch = fgets (myfile, 4);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1139 @end example