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