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