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