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