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