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