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