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