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