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 |
|
34 @deffn {Command} more |
|
35 @deffnx {Command} more on |
|
36 @deffnx {Command} more off |
|
37 Turn output pagination on or off. Without an argument, @code{more} |
|
38 toggles the current state. |
|
39 @end deffn |
|
40 |
|
41 @defvr {Built-in Variable} PAGER |
|
42 The default value is normally @code{"less"}, @code{"more"}, or |
|
43 @code{"pg"}, depending on what programs are installed on your system. |
|
44 @xref{Installation}. |
|
45 |
|
46 When running interactively, Octave sends any output intended for your |
|
47 terminal that is more than one screen long to the program named by the |
|
48 value of the variable @code{PAGER}. |
|
49 @end defvr |
|
50 |
|
51 @defvr {Built-in Variable} page_screen_output |
|
52 If the value of @code{page_screen_output} is nonzero, all output |
|
53 intended for the screen that is longer than one page is sent through a |
|
54 pager. This allows you to view one screenful at a time. Some pagers |
|
55 (such as @code{less}---see @ref{Installation}) are also capable of moving |
|
56 backward on the output. The default value is 1. |
|
57 @end defvr |
|
58 |
|
59 @defvr {Built-in Variable} page_output_immediately |
|
60 If the value of @code{page_output_immediately} is nonzero, Octave sends |
|
61 output to the pager as soon as it is available. Otherwise, Octave |
|
62 buffers its output and waits until just before the prompt is printed to |
|
63 flush it to the pager. The default value is 0. |
|
64 |
|
65 @deftypefn {Built-in Function} {} fflush (@var{fid}) |
|
66 Flush output to @var{fid}. This is useful for ensuring that all |
|
67 pending output makes it to the screen before some other event occurs. |
|
68 For example, it is always a good idea to flush the standard output |
|
69 stream before calling @code{input}. |
|
70 @end deftypefn |
|
71 |
|
72 @c XXX FIXME XXX -- maybe this would be a good place to describe the |
|
73 @c following message: |
|
74 @c |
|
75 @c warning: connection to external pager (pid = 9334) lost -- |
|
76 @c warning: pending computations and output may be lost |
|
77 @c warning: broken pipe |
|
78 @end defvr |
|
79 |
|
80 @menu |
|
81 * Basic Input and Output:: |
|
82 * C-Style I/O Functions:: |
|
83 @end menu |
|
84 |
|
85 @node Basic Input and Output, C-Style I/O Functions, Input and Output, Input and Output |
|
86 @section Basic Input and Output |
|
87 |
|
88 @menu |
|
89 * Terminal Output:: |
|
90 * Terminal Input:: |
|
91 * Simple File I/O:: |
|
92 @end menu |
|
93 |
|
94 @node Terminal Output, Terminal Input, Basic Input and Output, Basic Input and Output |
|
95 @subsection Terminal Output |
|
96 |
|
97 Since Octave normally prints the value of an expression as soon as it |
|
98 has been evaluated, the simplest of all I/O functions is a simple |
|
99 expression. For example, the following expression will display the |
|
100 value of pi |
|
101 |
|
102 @example |
|
103 pi |
|
104 @print{} pi = 3.1416 |
|
105 @end example |
|
106 |
|
107 This works well as long as it is acceptable to have the name of the |
|
108 variable (or @samp{ans}) printed along with the value. To print the |
|
109 value of a variable without printing its name, use the function |
|
110 @code{disp}. |
|
111 |
|
112 The @code{format} command offers some control over the way Octave prints |
|
113 values with @code{disp} and through the normal echoing mechanism. |
|
114 |
|
115 @defvr {Built-in Variable} ans |
|
116 This variable holds the most recently computed result that was not |
|
117 explicitly assigned to a variable. For example, after the expression |
|
118 |
|
119 @example |
|
120 3^2 + 4^2 |
|
121 @end example |
|
122 |
|
123 @noindent |
|
124 is evaluated, the value of @code{ans} is 25. |
|
125 @end defvr |
|
126 |
|
127 @deftypefn {Built-in Function} {} disp (@var{x}) |
|
128 Display the value of @var{x}. For example, |
|
129 |
|
130 @example |
|
131 disp ("The value of pi is:"), disp (pi) |
|
132 |
|
133 @print{} the value of pi is: |
|
134 @print{} 3.1416 |
|
135 @end example |
|
136 |
|
137 @noindent |
|
138 Note that the output from @code{disp} always ends with a newline. |
|
139 @end deftypefn |
|
140 |
|
141 @deffn {Command} format options |
|
142 Control the format of the output produced by @code{disp} and Octave's |
|
143 normal echoing mechanism. Valid options are listed in the following |
|
144 table. |
|
145 |
|
146 @table @code |
|
147 @item short |
|
148 Octave will try to print numbers with at |
|
149 least 3 significant figures within a field that is a maximum of 8 |
|
150 characters wide. |
|
151 |
|
152 If Octave is unable to format a matrix so that columns line up on the |
|
153 decimal point and all the numbers fit within the maximum field width, |
|
154 it switches to an @samp{e} format. |
|
155 |
|
156 @item long |
|
157 Octave will try to print numbers with at least 15 significant figures |
|
158 within a field that is a maximum of 24 characters wide. |
|
159 |
|
160 As will the @samp{short} format, Octave will switch to an @samp{e} |
|
161 format if it is unable to format a matrix so that columns line up on the |
|
162 decimal point and all the numbers fit within the maximum field width. |
|
163 |
|
164 @item long e |
|
165 @itemx short e |
|
166 The same as @samp{format long} or @samp{format short} but always display |
|
167 output with an @samp{e} format. For example, with the @samp{short e} |
|
168 format, pi is displayed as @code{3.14e+00}. |
|
169 |
|
170 @item long E |
|
171 @itemx short E |
|
172 The same as @samp{format long e} or @samp{format short e} but always |
|
173 display output with an uppercase @samp{E} format. For example, with |
|
174 the @samp{long E} format, pi is displayed as |
|
175 @code{3.14159265358979E+00}. |
|
176 |
|
177 @item free |
|
178 @itemx none |
|
179 Print output in free format, without trying to line up columns of |
|
180 matrices on the decimal point. This also causes complex numbers to be |
|
181 formatted like this @samp{(0.604194, 0.607088)} instead of like this |
|
182 @samp{0.60419 + 0.60709i}. |
|
183 |
|
184 @item bank |
|
185 Print in a fixed format with two places to the right of the decimal |
|
186 point. |
|
187 |
|
188 @item + |
|
189 Print a @samp{+} symbol for nonzero matrix elements and a space for zero |
|
190 matrix elements. This format can be very useful for examining the |
|
191 structure of a large matrix. |
|
192 |
|
193 @item hex |
|
194 Print the hexadecimal representation numbers as they are stored in |
|
195 memory. For example, on a workstation which stores 8 byte real values |
|
196 in IEEE format with the least significant byte first, the value of |
|
197 @code{pi} when printed in @code{hex} format is @code{400921fb54442d18}. |
|
198 This format only works for numeric values. |
|
199 |
|
200 @item bit |
|
201 Print the bit representation of numbers as stored in memory. |
|
202 For example, the value of @code{pi} is |
|
203 |
|
204 @example |
|
205 @group |
|
206 01000000000010010010000111111011 |
|
207 01010100010001000010110100011000 |
|
208 @end group |
|
209 @end example |
|
210 |
|
211 (shown here in two 32 bit sections for typesetting purposes) when |
|
212 printed in bit format on a workstation which stores 8 byte real values |
|
213 in IEEE format with the least significant byte first. This format only |
|
214 works for numeric types. |
|
215 @end table |
|
216 |
|
217 By default, Octave will try to print numbers with at least 5 significant |
|
218 figures within a field that is a maximum of 10 characters wide. |
|
219 |
|
220 If Octave is unable to format a matrix so that columns line up on the |
|
221 decimal point and all the numbers fit within the maximum field width, |
|
222 it switches to an @samp{e} format. |
|
223 |
|
224 If @code{format} is invoked without any options, the default format |
|
225 state is restored. |
|
226 @end deffn |
|
227 |
|
228 @defvr {Built-in Variable} print_answer_id_name |
|
229 If the value of @code{print_answer_id_name} is nonzero, variable |
|
230 names are printed along with the result. Otherwise, only the result |
|
231 values are printed. The default value is 1. |
|
232 @end defvr |
|
233 |
|
234 @node Terminal Input, Simple File I/O, Terminal Output, Basic Input and Output |
|
235 @subsection Terminal Input |
|
236 |
|
237 Octave has three functions that make it easy to prompt users for |
|
238 input. The @code{input} and @code{menu} functions are normally |
|
239 used for managing an interactive dialog with a user, and the |
|
240 @code{keyboard} function is normally used for doing simple debugging. |
|
241 |
|
242 @deftypefn {Built-in Function} {} input (@var{prompt}) |
|
243 @deftypefnx {Built-in Function} {} input (@var{prompt}, "s") |
|
244 Print a prompt and wait for user input. For example, |
|
245 |
|
246 @example |
|
247 input ("Pick a number, any number! ") |
|
248 @end example |
|
249 |
|
250 @noindent |
|
251 prints the prompt |
|
252 |
|
253 @example |
|
254 Pick a number, any number! |
|
255 @end example |
|
256 |
|
257 @noindent |
|
258 and waits for the user to enter a value. The string entered by the user |
|
259 is evaluated as an expression, so it may be a literal constant, a |
|
260 variable name, or any other valid expression. |
|
261 |
|
262 Currently, @code{input} only returns one value, regardless of the number |
|
263 of values produced by the evaluation of the expression. |
|
264 |
|
265 If you are only interested in getting a literal string value, you can |
|
266 call @code{input} with the character string @code{"s"} as the second |
|
267 argument. This tells Octave to return the string entered by the user |
|
268 directly, without evaluating it first. |
|
269 |
|
270 Because there may be output waiting to be displayed by the pager, it is |
|
271 a good idea to always call @code{fflush (stdout)} before calling |
|
272 @code{input}. This will ensure that all pending output is written to |
|
273 the screen before your prompt. @xref{Input and Output}. |
|
274 @end deftypefn |
|
275 |
|
276 @deftypefn {Function File} {} menu (@var{title}, @var{opt1}, @dots{}) |
|
277 Print a title string followed by a series of options. Each option will |
|
278 be printed along with a number. The return value is the number of the |
|
279 option selected by the user. This function is useful for interactive |
|
280 programs. There is no limit to the number of options that may be passed |
|
281 in, but it may be confusing to present more than will fit easily on one |
|
282 screen. |
|
283 @end deftypefn |
|
284 |
|
285 @deftypefn {Built-in Function} {} keyboard (@var{prompt}) |
|
286 This function is normally used for simple debugging. When the |
|
287 @code{keyboard} function is executed, Octave prints a prompt and waits |
|
288 for user input. The input strings are then evaluated and the results |
|
289 are printed. This makes it possible to examine the values of variables |
|
290 within a function, and to assign new values to variables. No value is |
|
291 returned from the @code{keyboard} function, and it continues to prompt |
|
292 for input until the user types @samp{quit}, or @samp{exit}. |
|
293 |
|
294 If @code{keyboard} is invoked without any arguments, a default prompt of |
|
295 @samp{debug> } is used. |
|
296 @end deftypefn |
|
297 |
|
298 For both @code{input} and @code{keyboard}, the normal command line |
|
299 history and editing functions are available at the prompt. |
|
300 |
|
301 Octave also has a function that makes it possible to get a single |
|
302 character from the keyboard without requiring the user to type a |
|
303 carriage return. |
|
304 |
|
305 @c XXX FIXME XXX -- perhaps kbhit should also be able to print a prompt |
|
306 @c string? |
|
307 |
|
308 @deftypefn {Built-in Function} {} kbhit () |
|
309 Read a single keystroke from the keyboard. For example, |
|
310 |
|
311 @example |
|
312 x = kbhit (); |
|
313 @end example |
|
314 |
|
315 @noindent |
|
316 will set @var{x} to the next character typed at the keyboard as soon as |
|
317 it is typed. |
|
318 @end deftypefn |
|
319 |
|
320 @node Simple File I/O, , Terminal Input, Basic Input and Output |
|
321 @subsection Simple File I/O |
|
322 |
|
323 The @code{save} and @code{load} commands allow data to be written to and |
|
324 read from disk files in various formats. The default format of files |
|
325 written by the @code{save} command can be controlled using the built-in |
|
326 variables @code{default_save_format} and @code{save_precision}. |
|
327 |
|
328 Note that Octave can not yet save or load structure variables or any |
|
329 user-defined types. |
|
330 |
|
331 @deffn {Command} save options file v1 v2 @dots{} |
|
332 Save the named variables @var{v1}, @var{v2}, @dots{} in the file |
|
333 @var{file}. The special filename @samp{-} can be used to write the |
|
334 output to your terminal. If no variable names are listed, Octave saves |
|
335 all the variables in the current scope. Valid options for the |
|
336 @code{save} command are listed in the following table. Options that |
|
337 modify the output format override the format specified by the built-in |
|
338 variable @code{default_save_format}. |
|
339 |
|
340 @table @code |
|
341 @item -ascii |
|
342 Save the data in Octave's text data format. |
|
343 |
|
344 @item -binary |
|
345 Save the data in Octave's binary data format. |
|
346 |
|
347 @item -float-binary |
|
348 Save the data in Octave's binary data format but only using single |
|
349 precision. You should use this format only if you know that all the |
|
350 values to be saved can be represented in single precision. |
|
351 |
|
352 @item -mat-binary |
|
353 Save the data in @sc{Matlab}'s binary data format. |
|
354 |
|
355 @item -save-builtins |
|
356 Force Octave to save the values of built-in variables too. By default, |
|
357 Octave does not save built-in variables. |
|
358 @end table |
|
359 |
|
360 The list of variables to save may include wildcard patterns containing |
|
361 the following special characters: |
|
362 @table @code |
|
363 @item ? |
|
364 Match any single character. |
|
365 |
|
366 @item * |
|
367 Match zero or more characters. |
|
368 |
|
369 @item [ @var{list} ] |
|
370 Match the list of characters specified by @var{list}. If the first |
|
371 character is @code{!} or @code{^}, match all characters except those |
|
372 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will |
|
373 match all lower and upper case alphabetic characters. |
|
374 @end table |
|
375 |
|
376 Except when using the @sc{Matlab} binary data file format, saving global |
|
377 variables also saves the global status of the variable, so that if it is |
|
378 restored at a later time using @samp{load}, it will be restored as a |
|
379 global variable. |
|
380 |
|
381 The command |
|
382 |
|
383 @example |
|
384 save -binary data a b* |
|
385 @end example |
|
386 |
|
387 @noindent |
|
388 saves the variable @samp{a} and all variables beginning with @samp{b} to |
|
389 the file @file{data} in Octave's binary format. |
|
390 @end deffn |
|
391 |
|
392 There are two variables that modify the behavior of @code{save} and one |
|
393 that controls whether variables are saved when Octave exits unexpectedly. |
|
394 |
|
395 @defvr {Built-in Variable} crash_dumps_octave_core |
|
396 If this variable is set to a nonzero value, Octave tries to save all |
|
397 current variables the the file "octave-core" if it crashes or receives a |
|
398 hangup, terminate or similar signal. The default value is 1. |
|
399 @end defvr |
|
400 |
|
401 @defvr {Built-in Variable} default_save_format |
|
402 This variable specifies the default format for the @code{save} command. |
|
403 It should have one of the following values: @code{"ascii"}, |
|
404 @code{"binary"}, @code{float-binary}, or @code{"mat-binary"}. The |
|
405 initial default save format is Octave's text format. |
|
406 @end defvr |
|
407 |
|
408 @defvr {Built-in Variable} save_precision |
|
409 This variable specifies the number of digits to keep when saving data in |
|
410 text format. The default value is 17. |
|
411 @end defvr |
|
412 |
|
413 @deffn {Command} load options file v1 v2 @dots{} |
|
414 Load the named variables from the file @var{file}. As with @code{save}, |
|
415 you may specify a list of variables and @code{load} will only extract |
|
416 those variables with names that match. For example, to restore the |
|
417 variables saved in the file @file{data}, use the command |
|
418 |
|
419 @example |
|
420 load data |
|
421 @end example |
|
422 |
|
423 Octave will refuse to overwrite existing variables unless you use the |
|
424 option @samp{-force}. |
|
425 |
|
426 If a variable that is not marked as global is loaded from a file when a |
|
427 global symbol with the same name already exists, it is loaded in the |
|
428 global symbol table. Also, if a variable is marked as global in a file |
|
429 and a local symbol exists, the local symbol is moved to the global |
|
430 symbol table and given the value from the file. Since it seems that |
|
431 both of these cases are likely to be the result of some sort of error, |
|
432 they will generate warnings. |
|
433 |
|
434 The @code{load} command can read data stored in Octave's text and |
|
435 binary formats, and @sc{Matlab}'s binary format. It will automatically |
|
436 detect the type of file and do conversion from different floating point |
|
437 formats (currently only IEEE big and little endian, though other formats |
|
438 may added in the future). |
|
439 |
|
440 Valid options for @code{load} are listed in the following table. |
|
441 |
|
442 @table @code |
|
443 @item -force |
|
444 Force variables currently in memory to be overwritten by variables with |
|
445 the same name found in the file. |
|
446 |
|
447 @item -ascii |
|
448 Force Octave to assume the file is in Octave's text format. |
|
449 |
|
450 @item -binary |
|
451 Force Octave to assume the file is in Octave's binary format. |
|
452 |
|
453 @item -mat-binary |
|
454 Force Octave to assume the file is in @sc{Matlab}'s binary format. |
|
455 @end table |
|
456 @end deffn |
|
457 |
|
458 @node C-Style I/O Functions, , Basic Input and Output, Input and Output |
|
459 @section C-Style I/O Functions |
|
460 |
|
461 Octave's C-style input and output functions provide most of the |
|
462 functionality of the C programming language's standard I/O library. The |
|
463 argument lists for some of the input functions are slightly different, |
|
464 however, because Octave has no way of passing arguments by reference. |
|
465 |
|
466 In the following, @var{file} refers to a file name and @code{fid} refers |
|
467 to an integer file number, as returned by @code{fopen}. |
|
468 |
|
469 There are three files that are always available. Although these files |
|
470 can be accessed using their corresponding numeric file ids, you should |
|
471 always use the symbolic names given in the table below, since it will |
|
472 make your programs easier to understand. |
|
473 |
|
474 @defvr {Built-in Variable} stdin |
|
475 The standard input stream (file id 0). When Octave is used |
|
476 interactively, this is filtered through the command line editing |
|
477 functions. |
|
478 @end defvr |
|
479 |
|
480 @defvr {Built-in Variable} stdout |
|
481 The standard output stream (file id 1). Data written to the |
|
482 standard output is normally filtered through the pager. |
|
483 @end defvr |
|
484 |
|
485 @defvr {Built-in Variable} stderr |
|
486 The standard error stream (file id 2). Even if paging is turned on, |
|
487 the standard error is not sent to the pager. It is useful for error |
|
488 messages and prompts. |
|
489 @end defvr |
|
490 |
|
491 @menu |
|
492 * Opening and Closing Files:: |
|
493 * Simple Output:: |
|
494 * Line-Oriented Input:: |
|
495 * Formatted Output:: |
|
496 * Output Conversion for Matrices:: |
|
497 * Output Conversion Syntax:: |
|
498 * Table of Output Conversions:: |
|
499 * Integer Conversions:: |
|
500 * Floating-Point Conversions:: Other Output Conversions:: |
|
501 * Other Output Conversions:: |
|
502 * Formatted Input:: |
|
503 * Input Conversion Syntax:: |
|
504 * Table of Input Conversions:: |
|
505 * Numeric Input Conversions:: |
|
506 * String Input Conversions:: |
|
507 * Binary I/O:: |
|
508 * Temporary Files:: |
|
509 * EOF and Errors:: |
|
510 * File Positioning:: |
|
511 @end menu |
|
512 |
|
513 @node Opening and Closing Files, Simple Output, C-Style I/O Functions, C-Style I/O Functions |
|
514 @subsection Opening and Closing Files |
|
515 |
|
516 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} fopen (@var{name}, @var{mode}, @var{arch}) |
|
517 @deftypefnx {Built-in Function} {@var{fid_list} =} fopen ("all") |
|
518 @deftypefnx {Built-in Function} {@var{file} =} fopen (@var{fid}) |
|
519 The first form of the @code{fopen} function opens the named file with |
|
520 the specified mode (read-write, read-only, etc.) and architecture |
|
521 interpretation (IEEE big endian, IEEE little endian, etc.), and returns |
|
522 an integer value that may be used to refer to the file later. If an |
|
523 error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the |
|
524 corresponding system error message. The @var{mode} is a one or two |
|
525 character string that specifies whether the file is to be opened for |
|
526 reading, writing, or both. |
|
527 |
|
528 The second form of the @code{fopen} function returns a vector of file ids |
|
529 corresponding to all the currently open files, excluding the |
|
530 @code{stdin}, @code{stdout}, and @code{stderr} streams. |
|
531 |
|
532 The third form of the @code{fopen} function returns the name of a |
|
533 currently open file given its file id. |
|
534 |
|
535 For example, |
|
536 |
|
537 @example |
|
538 myfile = fopen ("splat.dat", "r", "ieee-le"); |
|
539 @end example |
|
540 |
|
541 @noindent |
|
542 opens the file @file{splat.dat} for reading. If necessary, binary |
|
543 numeric values will be read assuming they are stored in IEEE format with |
|
544 the least significant bit first, and then converted to the native |
|
545 representation. |
|
546 |
|
547 Opening a file that is already open simply opens it again and returns a |
|
548 separate file id. It is not an error to open a file several times, |
|
549 though writing to the same file through several different file ids may |
|
550 produce unexpected results. |
|
551 |
|
552 The possible values @samp{mode} may have are |
|
553 |
|
554 @table @asis |
|
555 @item @samp{r} |
|
556 Open a file for reading. |
|
557 |
|
558 @item @samp{w} |
|
559 Open a file for writing. The previous contents are discared. |
|
560 |
|
561 @item @samp{a} |
|
562 Open or create a file for writing at the end of the file. |
|
563 |
|
564 @item @samp{r+} |
|
565 Open an existing file for reading and writing. |
|
566 |
|
567 @item @samp{w+} |
|
568 Open a file for reading or writing. The previous contents are |
|
569 discarded. |
|
570 |
|
571 @item @samp{a+} |
|
572 Open or create a file for reading or writing at the end of the |
|
573 file. |
|
574 @end table |
|
575 |
|
576 The parameter @var{arch} is a string specifying the default data format |
|
577 for the file. Valid values for @var{arch} are: |
|
578 |
|
579 @table @asis |
|
580 @samp{native} |
|
581 The format of the current machine (this is the default). |
|
582 |
|
583 @samp{ieee-le} |
|
584 IEEE big endian format. |
|
585 |
|
586 @samp{ieee-be} |
|
587 IEEE little endian format. |
|
588 |
|
589 @samp{vaxd} |
|
590 VAX D floating format. |
|
591 |
|
592 @samp{vaxg} |
|
593 VAX G floating format. |
|
594 |
|
595 @samp{cray} |
|
596 Cray floating format. |
|
597 @end table |
|
598 |
|
599 @noindent |
|
600 however, conversions are currently only supported for @samp{native} |
|
601 @samp{ieee-be}, and @samp{ieee-le} formats. |
|
602 @end deftypefn |
|
603 |
|
604 @deftypefn {Built-in Function} {} fclose (@var{fid}) |
|
605 Closes the specified file. If an error is encountered while trying to |
|
606 close the file, an error message is printed and @code{fclose} returns |
|
607 0. Otherwise, it returns 1. |
|
608 @end deftypefn |
|
609 |
|
610 @node Simple Output, Line-Oriented Input, Opening and Closing Files, C-Style I/O Functions |
|
611 @subsection Simple Output |
|
612 |
|
613 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string}) |
|
614 Write a string to a file with no formatting. |
|
615 @end deftypefn |
|
616 |
|
617 @deftypefn {Built-in Function} {} puts (@var{string}) |
|
618 Write a string to the standard output with no formatting. |
|
619 @end deftypefn |
|
620 |
|
621 @node Line-Oriented Input, Formatted Output, Simple Output, C-Style I/O Functions |
|
622 @subsection Line-Oriented Input |
|
623 |
|
624 @deftypefn {Built-in Function} {} fgetl (@var{fid}, @var{len}) |
|
625 Read characters from a file, stopping after a newline, or EOF, |
|
626 or @var{len} characters have been read. The characters read, excluding |
|
627 the possible trailing newline, are returned as a string. |
|
628 |
|
629 If @var{len} is omitted, @code{fgetl} reads until the next newline |
|
630 character. |
|
631 |
|
632 If there are no more characters to read, @code{fgetl} returns @minus{}1. |
|
633 @end deftypefn |
|
634 |
|
635 @deftypefn {Built-in Function} {} fgets (@var{fid}, @var{len}) |
|
636 Read characters from a file, stopping after a newline, or EOF, |
|
637 or @var{len} characters have been read. The characters read, including |
|
638 the possible trailing newline, are returned as a string. |
|
639 |
|
640 If @var{len} is omitted, @code{fgets} reads until the next newline |
|
641 character. |
|
642 |
|
643 If there are no more characters to read, @code{fgets} returns @minus{}1. |
|
644 @end deftypefn |
|
645 |
|
646 @node Formatted Output, Output Conversion for Matrices, Line-Oriented Input, C-Style I/O Functions |
|
647 @subsection Formatted Output |
|
648 |
|
649 This section describes how to call @code{printf} and related functions. |
|
650 |
|
651 The following functions are available for formatted output. They are |
|
652 modelled after the C language functions of the same name, but they |
|
653 interpret the format template differently in order to improve the |
|
654 performance of printing vector and matrix values. |
|
655 |
|
656 @deftypefn {Function File} {} printf (@var{template}, @dots{}) |
|
657 The @code{printf} function prints the optional arguments under the |
|
658 control of the template string @var{template} to the stream |
|
659 @code{stdout}. |
|
660 @end deftypefn |
|
661 |
|
662 @deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{}) |
|
663 This function is just like @code{printf}, except that the output is |
|
664 written to the stream @var{fid} instead of @code{stdout}. |
|
665 @end deftypefn |
|
666 |
|
667 @deftypefn {Built-in Function} {} sprintf (@var{template}, @dots{}) |
|
668 This is like @code{printf}, except that the output is returned as a |
|
669 string. Unlike the C library function, which requires you to provide a |
|
670 suitably sized string as an argument, Octave's @code{sprintf} function |
|
671 returns the string, automatically sized to hold all of the items |
|
672 converted. |
|
673 @end deftypefn |
|
674 |
|
675 The @code{printf} function can be used to print any number of arguments. |
|
676 The template string argument you supply in a call provides |
|
677 information not only about the number of additional arguments, but also |
|
678 about their types and what style should be used for printing them. |
|
679 |
|
680 Ordinary characters in the template string are simply written to the |
|
681 output stream as-is, while @dfn{conversion specifications} introduced by |
|
682 a @samp{%} character in the template cause subsequent arguments to be |
|
683 formatted and written to the output stream. For example, |
|
684 @cindex conversion specifications (@code{printf}) |
|
685 |
|
686 @smallexample |
|
687 pct = 37; |
|
688 filename = "foo.txt"; |
|
689 printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n", |
|
690 filename, pct); |
|
691 @end smallexample |
|
692 |
|
693 @noindent |
|
694 produces output like |
|
695 |
|
696 @smallexample |
|
697 Processing of `foo.txt' is 37% finished. |
|
698 Please be patient. |
|
699 @end smallexample |
|
700 |
|
701 This example shows the use of the @samp{%d} conversion to specify that a |
|
702 scalar argument should be printed in decimal notation, the @samp{%s} |
|
703 conversion to specify printing of a string argument, and the @samp{%%} |
|
704 conversion to print a literal @samp{%} character. |
|
705 |
|
706 There are also conversions for printing an integer argument as an |
|
707 unsigned value in octal, decimal, or hexadecimal radix (@samp{%o}, |
|
708 @samp{%u}, or @samp{%x}, respectively); or as a character value |
|
709 (@samp{%c}). |
|
710 |
|
711 Floating-point numbers can be printed in normal, fixed-point notation |
|
712 using the @samp{%f} conversion or in exponential notation using the |
|
713 @samp{%e} conversion. The @samp{%g} conversion uses either @samp{%e} |
|
714 or @samp{%f} format, depending on what is more appropriate for the |
|
715 magnitude of the particular number. |
|
716 |
|
717 You can control formatting more precisely by writing @dfn{modifiers} |
|
718 between the @samp{%} and the character that indicates which conversion |
|
719 to apply. These slightly alter the ordinary behavior of the conversion. |
|
720 For example, most conversion specifications permit you to specify a |
|
721 minimum field width and a flag indicating whether you want the result |
|
722 left- or right-justified within the field. |
|
723 |
|
724 The specific flags and modifiers that are permitted and their |
|
725 interpretation vary depending on the particular conversion. They're all |
|
726 described in more detail in the following sections. |
|
727 |
|
728 @node Output Conversion for Matrices, Output Conversion Syntax, Formatted Output, C-Style I/O Functions |
|
729 @subsection Output Conversion for Matrices |
|
730 |
|
731 When given a matrix value, Octave's formatted output functions cycle |
|
732 through the format template until all the values in the matrix have been |
|
733 printed. For example, |
|
734 |
|
735 @example |
|
736 @group |
|
737 printf ("%4.2f %10.2e %8.4g\n", hilb (3)); |
|
738 |
|
739 @print{} 1.00 5.00e-01 0.3333 |
|
740 @print{} 0.50 3.33e-01 0.25 |
|
741 @print{} 0.33 2.50e-01 0.2 |
|
742 @end group |
|
743 @end example |
|
744 |
|
745 If more than one value is to be printed in a single call, the output |
|
746 functions do not return to the beginning of the format template when |
|
747 moving on from one value to the next. This can lead to confusing output |
|
748 if the number of elements in the matrices are not exact multiples of the |
|
749 number of conversions in the format template. For example, |
|
750 |
|
751 @example |
|
752 @group |
|
753 printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]); |
|
754 |
|
755 @print{} 1.00 2.00e+00 3 |
|
756 @print{} 4.00 |
|
757 @end group |
|
758 @end example |
|
759 |
|
760 If this is not what you want, use a series of calls instead of just one. |
|
761 |
|
762 @node Output Conversion Syntax, Table of Output Conversions, Output Conversion for Matrices, C-Style I/O Functions |
|
763 @subsection Output Conversion Syntax |
|
764 |
|
765 This section provides details about the precise syntax of conversion |
|
766 specifications that can appear in a @code{printf} template |
|
767 string. |
|
768 |
|
769 Characters in the template string that are not part of a |
|
770 conversion specification are printed as-is to the output stream. |
|
771 |
|
772 The conversion specifications in a @code{printf} template string have |
|
773 the general form: |
|
774 |
|
775 @smallexample |
|
776 % @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion} |
|
777 @end smallexample |
|
778 |
|
779 For example, in the conversion specifier @samp{%-10.8ld}, the @samp{-} |
|
780 is a flag, @samp{10} specifies the field width, the precision is |
|
781 @samp{8}, the letter @samp{l} is a type modifier, and @samp{d} specifies |
|
782 the conversion style. (This particular type specifier says to print a |
|
783 numeric argument in decimal notation, with a minimum of 8 digits |
|
784 left-justified in a field at least 10 characters wide.) |
|
785 |
|
786 In more detail, output conversion specifications consist of an |
|
787 initial @samp{%} character followed in sequence by: |
|
788 |
|
789 @itemize @bullet |
|
790 @item |
|
791 Zero or more @dfn{flag characters} that modify the normal behavior of |
|
792 the conversion specification. |
|
793 @cindex flag character (@code{printf}) |
|
794 |
|
795 @item |
|
796 An optional decimal integer specifying the @dfn{minimum field width}. |
|
797 If the normal conversion produces fewer characters than this, the field |
|
798 is padded with spaces to the specified width. This is a @emph{minimum} |
|
799 value; if the normal conversion produces more characters than this, the |
|
800 field is @emph{not} truncated. Normally, the output is right-justified |
|
801 within the field. |
|
802 @cindex minimum field width (@code{printf}) |
|
803 |
|
804 You can also specify a field width of @samp{*}. This means that the |
|
805 next argument in the argument list (before the actual value to be |
|
806 printed) is used as the field width. The value is rounded to the |
|
807 nearest integer. If the value is negative, this means to set the |
|
808 @samp{-} flag (see below) and to use the absolute value as the field |
|
809 width. |
|
810 |
|
811 @item |
|
812 An optional @dfn{precision} to specify the number of digits to be |
|
813 written for the numeric conversions. If the precision is specified, it |
|
814 consists of a period (@samp{.}) followed optionally by a decimal integer |
|
815 (which defaults to zero if omitted). |
|
816 @cindex precision (@code{printf}) |
|
817 |
|
818 You can also specify a precision of @samp{*}. This means that the next |
|
819 argument in the argument list (before the actual value to be printed) is |
|
820 used as the precision. The value must be an integer, and is ignored |
|
821 if it is negative. |
|
822 |
|
823 @item |
|
824 An optional @dfn{type modifier character}. This character is ignored by |
|
825 Octave's @code{printf} function, but is recognized to provide |
|
826 compatibility with the C language @code{printf}. |
|
827 |
|
828 @item |
|
829 A character that specifies the conversion to be applied. |
|
830 @end itemize |
|
831 |
|
832 The exact options that are permitted and how they are interpreted vary |
|
833 between the different conversion specifiers. See the descriptions of the |
|
834 individual conversions for information about the particular options that |
|
835 they use. |
|
836 |
|
837 @node Table of Output Conversions, Integer Conversions, Output Conversion Syntax, C-Style I/O Functions |
|
838 @subsection Table of Output Conversions |
|
839 @cindex output conversions, for @code{printf} |
|
840 |
|
841 Here is a table summarizing what all the different conversions do: |
|
842 |
|
843 @table @asis |
|
844 @item @samp{%d}, @samp{%i} |
|
845 Print an integer as a signed decimal number. @xref{Integer |
|
846 Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for |
|
847 output, but are different when used with @code{scanf} for input |
|
848 (@pxref{Table of Input Conversions}). |
|
849 |
|
850 @item @samp{%o} |
|
851 Print an integer as an unsigned octal number. @xref{Integer |
|
852 Conversions}, for details. |
|
853 |
|
854 @item @samp{%u} |
|
855 Print an integer as an unsigned decimal number. @xref{Integer |
|
856 Conversions}, for details. |
|
857 |
|
858 @item @samp{%x}, @samp{%X} |
|
859 Print an integer as an unsigned hexadecimal number. @samp{%x} uses |
|
860 lower-case letters and @samp{%X} uses upper-case. @xref{Integer |
|
861 Conversions}, for details. |
|
862 |
|
863 @item @samp{%f} |
|
864 Print a floating-point number in normal (fixed-point) notation. |
|
865 @xref{Floating-Point Conversions}, for details. |
|
866 |
|
867 @item @samp{%e}, @samp{%E} |
|
868 Print a floating-point number in exponential notation. @samp{%e} uses |
|
869 lower-case letters and @samp{%E} uses upper-case. @xref{Floating-Point |
|
870 Conversions}, for details. |
|
871 |
|
872 @item @samp{%g}, @samp{%G} |
|
873 Print a floating-point number in either normal (fixed-point) or |
|
874 exponential notation, whichever is more appropriate for its magnitude. |
|
875 @samp{%g} uses lower-case letters and @samp{%G} uses upper-case. |
|
876 @xref{Floating-Point Conversions}, for details. |
|
877 |
|
878 @item @samp{%c} |
|
879 Print a single character. @xref{Other Output Conversions}. |
|
880 |
|
881 @item @samp{%s} |
|
882 Print a string. @xref{Other Output Conversions}. |
|
883 |
|
884 @item @samp{%%} |
|
885 Print a literal @samp{%} character. @xref{Other Output Conversions}. |
|
886 @end table |
|
887 |
|
888 If the syntax of a conversion specification is invalid, unpredictable |
|
889 things will happen, so don't do this. If there aren't enough function |
|
890 arguments provided to supply values for all the conversion |
|
891 specifications in the template string, or if the arguments are not of |
|
892 the correct types, the results are unpredictable. If you supply more |
|
893 arguments than conversion specifications, the extra argument values are |
|
894 simply ignored; this is sometimes useful. |
|
895 |
|
896 @node Integer Conversions, Floating-Point Conversions, Table of Output Conversions, C-Style I/O Functions |
|
897 @subsection Integer Conversions |
|
898 |
|
899 This section describes the options for the @samp{%d}, @samp{%i}, |
|
900 @samp{%o}, @samp{%u}, @samp{%x}, and @samp{%X} conversion |
|
901 specifications. These conversions print integers in various formats. |
|
902 |
|
903 The @samp{%d} and @samp{%i} conversion specifications both print an |
|
904 numeric argument as a signed decimal number; while @samp{%o}, |
|
905 @samp{%u}, and @samp{%x} print the argument as an unsigned octal, |
|
906 decimal, or hexadecimal number (respectively). The @samp{%X} conversion |
|
907 specification is just like @samp{%x} except that it uses the characters |
|
908 @samp{ABCDEF} as digits instead of @samp{abcdef}. |
|
909 |
|
910 The following flags are meaningful: |
|
911 |
|
912 @table @asis |
|
913 @item @samp{-} |
|
914 Left-justify the result in the field (instead of the normal |
|
915 right-justification). |
|
916 |
|
917 @item @samp{+} |
|
918 For the signed @samp{%d} and @samp{%i} conversions, print a |
|
919 plus sign if the value is positive. |
|
920 |
|
921 @item @samp{ } |
|
922 For the signed @samp{%d} and @samp{%i} conversions, if the result |
|
923 doesn't start with a plus or minus sign, prefix it with a space |
|
924 character instead. Since the @samp{+} flag ensures that the result |
|
925 includes a sign, this flag is ignored if you supply both of them. |
|
926 |
|
927 @item @samp{#} |
|
928 For the @samp{%o} conversion, this forces the leading digit to be |
|
929 @samp{0}, as if by increasing the precision. For @samp{%x} or |
|
930 @samp{%X}, this prefixes a leading @samp{0x} or @samp{0X} (respectively) |
|
931 to the result. This doesn't do anything useful for the @samp{%d}, |
|
932 @samp{%i}, or @samp{%u} conversions. |
|
933 |
|
934 @item @samp{0} |
|
935 Pad the field with zeros instead of spaces. The zeros are placed after |
|
936 any indication of sign or base. This flag is ignored if the @samp{-} |
|
937 flag is also specified, or if a precision is specified. |
|
938 @end table |
|
939 |
|
940 If a precision is supplied, it specifies the minimum number of digits to |
|
941 appear; leading zeros are produced if necessary. If you don't specify a |
|
942 precision, the number is printed with as many digits as it needs. If |
|
943 you convert a value of zero with an explicit precision of zero, then no |
|
944 characters at all are produced. |
|
945 |
|
946 @node Floating-Point Conversions, Other Output Conversions, Integer Conversions, C-Style I/O Functions |
|
947 @subsection Floating-Point Conversions |
|
948 |
|
949 This section discusses the conversion specifications for floating-point |
|
950 numbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G} |
|
951 conversions. |
|
952 |
|
953 The @samp{%f} conversion prints its argument in fixed-point notation, |
|
954 producing output of the form |
|
955 @w{[@code{-}]@var{ddd}@code{.}@var{ddd}}, |
|
956 where the number of digits following the decimal point is controlled |
|
957 by the precision you specify. |
|
958 |
|
959 The @samp{%e} conversion prints its argument in exponential notation, |
|
960 producing output of the form |
|
961 @w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}. |
|
962 Again, the number of digits following the decimal point is controlled by |
|
963 the precision. The exponent always contains at least two digits. The |
|
964 @samp{%E} conversion is similar but the exponent is marked with the letter |
|
965 @samp{E} instead of @samp{e}. |
|
966 |
|
967 The @samp{%g} and @samp{%G} conversions print the argument in the style |
|
968 of @samp{%e} or @samp{%E} (respectively) if the exponent would be less |
|
969 than -4 or greater than or equal to the precision; otherwise they use the |
|
970 @samp{%f} style. Trailing zeros are removed from the fractional portion |
|
971 of the result and a decimal-point character appears only if it is |
|
972 followed by a digit. |
|
973 |
|
974 The following flags can be used to modify the behavior: |
|
975 |
|
976 @c Not @samp so we can have ` ' as an item. |
|
977 @table @asis |
|
978 @item @samp{-} |
|
979 Left-justify the result in the field. Normally the result is |
|
980 right-justified. |
|
981 |
|
982 @item @samp{+} |
|
983 Always include a plus or minus sign in the result. |
|
984 |
|
985 @item @samp{ } |
|
986 If the result doesn't start with a plus or minus sign, prefix it with a |
|
987 space instead. Since the @samp{+} flag ensures that the result includes |
|
988 a sign, this flag is ignored if you supply both of them. |
|
989 |
|
990 @item @samp{#} |
|
991 Specifies that the result should always include a decimal point, even |
|
992 if no digits follow it. For the @samp{%g} and @samp{%G} conversions, |
|
993 this also forces trailing zeros after the decimal point to be left |
|
994 in place where they would otherwise be removed. |
|
995 |
|
996 @item @samp{0} |
|
997 Pad the field with zeros instead of spaces; the zeros are placed |
|
998 after any sign. This flag is ignored if the @samp{-} flag is also |
|
999 specified. |
|
1000 @end table |
|
1001 |
|
1002 The precision specifies how many digits follow the decimal-point |
|
1003 character for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. For |
|
1004 these conversions, the default precision is @code{6}. If the precision |
|
1005 is explicitly @code{0}, this suppresses the decimal point character |
|
1006 entirely. For the @samp{%g} and @samp{%G} conversions, the precision |
|
1007 specifies how many significant digits to print. Significant digits are |
|
1008 the first digit before the decimal point, and all the digits after it. |
|
1009 If the precision is @code{0} or not specified for @samp{%g} or |
|
1010 @samp{%G}, it is treated like a value of @code{1}. If the value being |
|
1011 printed cannot be expressed precisely in the specified number of digits, |
|
1012 the value is rounded to the nearest number that fits. |
|
1013 |
|
1014 @node Other Output Conversions, Formatted Input, Floating-Point Conversions, C-Style I/O Functions |
|
1015 @subsection Other Output Conversions |
|
1016 |
|
1017 This section describes miscellaneous conversions for @code{printf}. |
|
1018 |
|
1019 The @samp{%c} conversion prints a single character. The @samp{-} |
|
1020 flag can be used to specify left-justification in the field, but no |
|
1021 other flags are defined, and no precision or type modifier can be given. |
|
1022 For example: |
|
1023 |
|
1024 @smallexample |
|
1025 printf ("%c%c%c%c%c", "h", "e", "l", "l", "o"); |
|
1026 @end smallexample |
|
1027 |
|
1028 @noindent |
|
1029 prints @samp{hello}. |
|
1030 |
|
1031 The @samp{%s} conversion prints a string. The corresponding argument |
|
1032 must be a string. A precision can be specified to indicate the maximum |
|
1033 number of characters to write; otherwise characters in the string up to |
|
1034 but not including the terminating null character are written to the |
|
1035 output stream. The @samp{-} flag can be used to specify |
|
1036 left-justification in the field, but no other flags or type modifiers |
|
1037 are defined for this conversion. For example: |
|
1038 |
|
1039 @smallexample |
|
1040 printf ("%3s%-6s", "no", "where"); |
|
1041 @end smallexample |
|
1042 |
|
1043 @noindent |
|
1044 prints @samp{ nowhere } (note the leading and trailing spaces). |
|
1045 |
|
1046 @node Formatted Input, Input Conversion Syntax, Other Output Conversions, C-Style I/O Functions |
|
1047 @subsection Formatted Input |
|
1048 |
|
1049 Octave provides the @code{scanf}, @code{fscanf}, and @code{sscanf} |
|
1050 functions to read formatted input. There are two forms of each of these |
|
1051 functions. One can be used to extract vectors of data from a file, and |
|
1052 the other is more `C-like'. |
|
1053 |
|
1054 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fscanf (@var{fid}, @var{template}, @var{size}) |
|
1055 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } fscanf (@var{fid}, @var{template}, "C") |
|
1056 In the first form, read from @var{fid} according to @var{template}, |
|
1057 returning the result in the matrix @var{val}. |
|
1058 |
|
1059 The optional argument @var{size} specifies the amount of data to read |
|
1060 and may be one of |
|
1061 |
|
1062 @table @code |
|
1063 @item Inf |
|
1064 Read as much as possible, returning a column vector. |
|
1065 |
|
1066 @item @var{nr} |
|
1067 Read up to @var{nr} elements, returning a column vector. |
|
1068 |
|
1069 @item [@var{nr}, Inf] |
|
1070 Read as much as possible, returning a matrix with @var{nr} rows. If the |
|
1071 number of elements read is not an exact multiple of @var{nr}, the last |
|
1072 column is padded with zeros. |
|
1073 |
|
1074 @item [@var{nr}, @var{nc}] |
|
1075 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with |
|
1076 @var{nr} rows. If the number of elements read is not an exact multiple |
|
1077 of @var{nr}, the last column is padded with zeros. |
|
1078 @end table |
|
1079 |
|
1080 @noindent |
|
1081 If @var{size} is omitted, a value of @code{Inf} is assumed. |
|
1082 |
|
1083 A string is returned if @var{template} specifies only character |
|
1084 conversions. |
|
1085 |
|
1086 The number of items successfully read is returned in @var{count}. |
|
1087 |
|
1088 In the second form, read from @var{fid} according to @var{template}, |
|
1089 with each conversion specifier in @var{template} corresponding to a |
|
1090 single scalar return value. This form is more `C-like', and also |
|
1091 compatible with previous versions of Octave. |
|
1092 @end deftypefn |
|
1093 |
|
1094 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} sscanf (@var{string}, @var{template}, @var{size}) |
|
1095 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } sscanf (@var{string}, @var{template}, "C") |
|
1096 This is like @code{fscanf}, except that the characters are taken from the |
|
1097 string @var{string} instead of from a stream. Reaching the end of the |
|
1098 string is treated as an end-of-file condition. |
|
1099 @end deftypefn |
|
1100 |
|
1101 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} scanf (@var{template}, @var{size}) |
|
1102 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } scanf (@var{template}, "C") |
|
1103 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}. |
|
1104 |
|
1105 It is currently not useful to call @code{scanf} in interactive |
|
1106 programs. |
|
1107 @end deftypefn |
|
1108 |
|
1109 Calls to @code{scanf} are superficially similar to calls to |
|
1110 @code{printf} in that arbitrary arguments are read under the control of |
|
1111 a template string. While the syntax of the conversion specifications in |
|
1112 the template is very similar to that for @code{printf}, the |
|
1113 interpretation of the template is oriented more towards free-format |
|
1114 input and simple pattern matching, rather than fixed-field formatting. |
|
1115 For example, most @code{scanf} conversions skip over any amount of |
|
1116 ``white space'' (including spaces, tabs, and newlines) in the input |
|
1117 file, and there is no concept of precision for the numeric input |
|
1118 conversions as there is for the corresponding output conversions. |
|
1119 Ordinarily, non-whitespace characters in the template are expected to |
|
1120 match characters in the input stream exactly. |
|
1121 @cindex conversion specifications (@code{scanf}) |
|
1122 |
|
1123 When a @dfn{matching failure} occurs, @code{scanf} returns immediately, |
|
1124 leaving the first non-matching character as the next character to be |
|
1125 read from the stream, and @code{scanf} returns all the items that were |
|
1126 successfully converted. |
|
1127 @cindex matching failure, in @code{scanf} |
|
1128 |
|
1129 The formatted input functions are not used as frequently as the |
|
1130 formatted output functions. Partly, this is because it takes some care |
|
1131 to use them properly. Another reason is that it is difficult to recover |
|
1132 from a matching error. |
|
1133 |
|
1134 @node Input Conversion Syntax, Table of Input Conversions, Formatted Input, C-Style I/O Functions |
|
1135 @subsection Input Conversion Syntax |
|
1136 |
|
1137 A @code{scanf} template string is a string that contains ordinary |
|
1138 multibyte characters interspersed with conversion specifications that |
|
1139 start with @samp{%}. |
|
1140 |
|
1141 Any whitespace character in the template causes any number of whitespace |
|
1142 characters in the input stream to be read and discarded. The whitespace |
|
1143 characters that are matched need not be exactly the same whitespace |
|
1144 characters that appear in the template string. For example, write |
|
1145 @samp{ , } in the template to recognize a comma with optional whitespace |
|
1146 before and after. |
|
1147 |
|
1148 Other characters in the template string that are not part of conversion |
|
1149 specifications must match characters in the input stream exactly; if |
|
1150 this is not the case, a matching failure occurs. |
|
1151 |
|
1152 The conversion specifications in a @code{scanf} template string |
|
1153 have the general form: |
|
1154 |
|
1155 @smallexample |
|
1156 % @var{flags} @var{width} @var{type} @var{conversion} |
|
1157 @end smallexample |
|
1158 |
|
1159 In more detail, an input conversion specification consists of an initial |
|
1160 @samp{%} character followed in sequence by: |
|
1161 |
|
1162 @itemize @bullet |
|
1163 @item |
|
1164 An optional @dfn{flag character} @samp{*}, which says to ignore the text |
|
1165 read for this specification. When @code{scanf} finds a conversion |
|
1166 specification that uses this flag, it reads input as directed by the |
|
1167 rest of the conversion specification, but it discards this input, does |
|
1168 not return any value, and does not increment the count of |
|
1169 successful assignments. |
|
1170 @cindex flag character (@code{scanf}) |
|
1171 |
|
1172 @item |
|
1173 An optional decimal integer that specifies the @dfn{maximum field |
|
1174 width}. Reading of characters from the input stream stops either when |
|
1175 this maximum is reached or when a non-matching character is found, |
|
1176 whichever happens first. Most conversions discard initial whitespace |
|
1177 characters, and these discarded characters don't count towards the |
|
1178 maximum field width. Conversions that do not discard initial whitespace |
|
1179 are explicitly documented. |
|
1180 @cindex maximum field width (@code{scanf}) |
|
1181 |
|
1182 @item |
|
1183 An optional type modifier character. This character is ignored by |
|
1184 Octave's @code{scanf} function, but is recognized to provide |
|
1185 compatibility with the C language @code{scanf}. |
|
1186 |
|
1187 @item |
|
1188 A character that specifies the conversion to be applied. |
|
1189 @end itemize |
|
1190 |
|
1191 The exact options that are permitted and how they are interpreted vary |
|
1192 between the different conversion specifiers. See the descriptions of the |
|
1193 individual conversions for information about the particular options that |
|
1194 they allow. |
|
1195 |
|
1196 @node Table of Input Conversions, Numeric Input Conversions, Input Conversion Syntax, C-Style I/O Functions |
|
1197 @subsection Table of Input Conversions |
|
1198 @cindex input conversions, for @code{scanf} |
|
1199 |
|
1200 Here is a table that summarizes the various conversion specifications: |
|
1201 |
|
1202 @table @asis |
|
1203 @item @samp{%d} |
|
1204 Matches an optionally signed integer written in decimal. @xref{Numeric |
|
1205 Input Conversions}. |
|
1206 |
|
1207 @item @samp{%i} |
|
1208 Matches an optionally signed integer in any of the formats that the C |
|
1209 language defines for specifying an integer constant. @xref{Numeric |
|
1210 Input Conversions}. |
|
1211 |
|
1212 @item @samp{%o} |
|
1213 Matches an unsigned integer written in octal radix. |
|
1214 @xref{Numeric Input Conversions}. |
|
1215 |
|
1216 @item @samp{%u} |
|
1217 Matches an unsigned integer written in decimal radix. |
|
1218 @xref{Numeric Input Conversions}. |
|
1219 |
|
1220 @item @samp{%x}, @samp{%X} |
|
1221 Matches an unsigned integer written in hexadecimal radix. |
|
1222 @xref{Numeric Input Conversions}. |
|
1223 |
|
1224 @item @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, @samp{%G} |
|
1225 Matches an optionally signed floating-point number. @xref{Numeric Input |
|
1226 Conversions}. |
|
1227 |
|
1228 @item @samp{%s} |
|
1229 Matches a string containing only non-whitespace characters. |
|
1230 @xref{String Input Conversions}. |
|
1231 |
|
1232 @item @samp{%c} |
|
1233 Matches a string of one or more characters; the number of characters |
|
1234 read is controlled by the maximum field width given for the conversion. |
|
1235 @xref{String Input Conversions}. |
|
1236 |
|
1237 @item @samp{%%} |
|
1238 This matches a literal @samp{%} character in the input stream. No |
|
1239 corresponding argument is used. |
|
1240 @end table |
|
1241 |
|
1242 If the syntax of a conversion specification is invalid, the behavior is |
|
1243 undefined. If there aren't enough function arguments provided to supply |
|
1244 addresses for all the conversion specifications in the template strings |
|
1245 that perform assignments, or if the arguments are not of the correct |
|
1246 types, the behavior is also undefined. On the other hand, extra |
|
1247 arguments are simply ignored. |
|
1248 |
|
1249 @node Numeric Input Conversions, String Input Conversions, Table of Input Conversions, C-Style I/O Functions |
|
1250 @subsection Numeric Input Conversions |
|
1251 |
|
1252 This section describes the @code{scanf} conversions for reading numeric |
|
1253 values. |
|
1254 |
|
1255 The @samp{%d} conversion matches an optionally signed integer in decimal |
|
1256 radix. |
|
1257 |
|
1258 The @samp{%i} conversion matches an optionally signed integer in any of |
|
1259 the formats that the C language defines for specifying an integer |
|
1260 constant. |
|
1261 |
|
1262 For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012} |
|
1263 could be read in as integers under the @samp{%i} conversion. Each of |
|
1264 these specifies a number with decimal value @code{10}. |
|
1265 |
|
1266 The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned |
|
1267 integers in octal, decimal, and hexadecimal radices, respectively. |
|
1268 |
|
1269 The @samp{%X} conversion is identical to the @samp{%x} conversion. They |
|
1270 both permit either uppercase or lowercase letters to be used as digits. |
|
1271 |
|
1272 Unlike the C language @code{scanf}, Octave ignores the @samp{h}, |
|
1273 @samp{l}, and @samp{L} modifiers. |
|
1274 |
|
1275 @node String Input Conversions, Binary I/O, Numeric Input Conversions, C-Style I/O Functions |
|
1276 @subsection String Input Conversions |
|
1277 |
|
1278 This section describes the @code{scanf} input conversions for reading |
|
1279 string and character values: @samp{%s} and @samp{%c}. |
|
1280 |
|
1281 The @samp{%c} conversion is the simplest: it matches a fixed number of |
|
1282 characters, always. The maximum field with says how many characters to |
|
1283 read; if you don't specify the maximum, the default is 1. This |
|
1284 conversion does not skip over initial whitespace characters. It reads |
|
1285 precisely the next @var{n} characters, and fails if it cannot get that |
|
1286 many. |
|
1287 |
|
1288 The @samp{%s} conversion matches a string of non-whitespace characters. |
|
1289 It skips and discards initial whitespace, but stops when it encounters |
|
1290 more whitespace after having read something. |
|
1291 |
|
1292 For example, reading the input: |
|
1293 |
|
1294 @smallexample |
|
1295 hello, world |
|
1296 @end smallexample |
|
1297 |
|
1298 @noindent |
|
1299 with the conversion @samp{%10c} produces @code{" hello, wo"}, but |
|
1300 reading the same input with the conversion @samp{%10s} produces |
|
1301 @code{"hello,"}. |
|
1302 |
|
1303 @node Binary I/O, Temporary Files, String Input Conversions, C-Style I/O Functions |
|
1304 @subsection Binary I/O |
|
1305 |
|
1306 Octave can read and write binary data using the functions @code{fread} |
|
1307 and @code{fwrite}, which are patterned after the standard C functions |
|
1308 with the same names. The are able to automatically swap the byte order |
|
1309 of integer data and convert among ths supported floating point formats |
|
1310 as the data are read. |
|
1311 |
|
1312 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch}) |
|
1313 Read binary data of type @var{precision} from the specified file ID |
|
1314 @var{fid}. |
|
1315 |
|
1316 The optional argument @var{size} specifies the amount of data to read |
|
1317 and may be one of |
|
1318 |
|
1319 @table @code |
|
1320 @item Inf |
|
1321 Read as much as possible, returning a column vector. |
|
1322 |
|
1323 @item @var{nr} |
|
1324 Read up to @var{nr} elements, returning a column vector. |
|
1325 |
|
1326 @item [@var{nr}, Inf] |
|
1327 Read as much as possible, returning a matrix with @var{nr} rows. If the |
|
1328 number of elements read is not an exact multiple of @var{nr}, the last |
|
1329 column is padded with zeros. |
|
1330 |
|
1331 @item [@var{nr}, @var{nc}] |
|
1332 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with |
|
1333 @var{nr} rows. If the number of elements read is not an exact multiple |
|
1334 of @var{nr}, the last column is padded with zeros. |
|
1335 @end table |
|
1336 |
|
1337 @noindent |
|
1338 If @var{size} is omitted, a value of @code{Inf} is assumed. |
|
1339 |
|
1340 The optional argument @var{precision} is a string specifying the type of |
|
1341 data to read and may be one of |
|
1342 |
|
1343 @table @code |
|
1344 @item "char" |
|
1345 @itemx "char*1" |
|
1346 @itemx "integer*1" |
|
1347 @itemx "int8" |
|
1348 Single character. |
|
1349 |
|
1350 @item "signed char" |
|
1351 @itemx "schar" |
|
1352 Signed character. |
|
1353 |
|
1354 @item "unsigned char" |
|
1355 @itemx "uchar" |
|
1356 Unsigned character. |
|
1357 |
|
1358 @item "short" |
|
1359 Short integer. |
|
1360 |
|
1361 @item "unsigned short" |
|
1362 @itemx "ushort" |
|
1363 Unsigned short integer. |
|
1364 |
|
1365 @item "int" |
|
1366 Integer. |
|
1367 |
|
1368 @item "unsigned int" |
|
1369 @itemx "uint" |
|
1370 Unsigned integer. |
|
1371 |
|
1372 @item "long" |
|
1373 Long integer. |
|
1374 |
|
1375 @item "unsigned long" |
|
1376 @itemx "ulong" |
|
1377 Unsigned long integer. |
|
1378 |
|
1379 @item "float" |
|
1380 @itemx "float32" |
|
1381 @itemx "real*4" |
|
1382 Single precision float. |
|
1383 |
|
1384 @item "double" |
|
1385 @itemx "float64" |
|
1386 @itemx "real*8" |
|
1387 Double precision float. |
|
1388 |
|
1389 @item "integer*2" |
|
1390 @itemx "int16" |
|
1391 Two byte integer. |
|
1392 |
|
1393 @item "integer*4" |
|
1394 @itemx "int32" |
|
1395 Four byte integer. |
|
1396 @end table |
|
1397 |
|
1398 @noindent |
|
1399 The default precision is @code{"uchar"}. |
|
1400 |
|
1401 The optional argument @var{skip} specifies the number of bytes to skip |
|
1402 before each element is read. If it is not specified, a value of 0 is |
|
1403 assumed. |
|
1404 |
|
1405 The optional argument @var{arch} is a string specifying the data format |
|
1406 for the file. Valid values are |
|
1407 |
|
1408 @table @code |
|
1409 @item "native" |
|
1410 The format of the current machine. |
|
1411 |
|
1412 @item "ieee-le" |
|
1413 IEEE big endian. |
|
1414 |
|
1415 @item "ieee-be" |
|
1416 IEEE little endian. |
|
1417 |
|
1418 @item "vaxd" |
|
1419 VAX D floating format. |
|
1420 |
|
1421 @item "vaxg" |
|
1422 VAX G floating format. |
|
1423 |
|
1424 @item "cray" |
|
1425 Cray floating format. |
|
1426 @end table |
|
1427 |
|
1428 @noindent |
|
1429 Conversions are currently only supported for @code{"ieee-be"} and |
|
1430 @code{"ieee-le"} formats. |
|
1431 |
|
1432 The data read from the file is returned in @var{val}, and the number of |
|
1433 values read is returned in @code{count} |
|
1434 @end deftypefn |
|
1435 |
|
1436 @deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch}) |
|
1437 Write data in binary form of type @var{precision} to the specified file |
|
1438 ID @var{fid}, returning the number of values successfully written to the |
|
1439 file. |
|
1440 |
|
1441 The argument @var{data} is a matrix of values that are to be written to |
|
1442 the file. The values are extracted in column-major order. |
|
1443 |
|
1444 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are |
|
1445 optional, and are interpreted as described for @code{fread}. |
|
1446 |
|
1447 The behavior of @code{fwrite} is undefined if the values in @var{data} |
|
1448 are too large to fit in the specified precision. |
|
1449 @end deftypefn |
|
1450 |
|
1451 @node Temporary Files, EOF and Errors, Binary I/O, C-Style I/O Functions |
|
1452 @subsection Temporary Files |
|
1453 |
|
1454 @deftypefn {Built-in Function} {} tmpnam () |
|
1455 Return a unique temporary file name as a string. |
|
1456 |
|
1457 Since the named file is not opened, by @code{tmpnam}, it |
|
1458 is possible (though relatively unlikely) that it will not be available |
|
1459 by the time your program attempts to open it. |
|
1460 @end deftypefn |
|
1461 |
|
1462 @node EOF and Errors, File Positioning, Temporary Files, C-Style I/O Functions |
|
1463 @subsection End of File and Errors |
|
1464 |
|
1465 @deftypefn {Built-in Function} {} feof (@var{fid}) |
|
1466 Return 1 if an end-of-file condition has been encountered for a given |
|
1467 file and 0 otherwise. Note that it will only return 1 if the end of the |
|
1468 file has already been encountered, not if the next read operation will |
|
1469 result in an end-of-file condition. |
|
1470 @end deftypefn |
|
1471 |
|
1472 @deftypefn {Built-in Function} {} ferror (@var{fid}) |
|
1473 Return 1 if an error condition has been encountered for a given file |
|
1474 and 0 otherwise. Note that it will only return 1 if an error has |
|
1475 already been encountered, not if the next operation will result in an |
|
1476 error condition. |
|
1477 @end deftypefn |
|
1478 |
|
1479 @deftypefn {Built-in Function} {} freport () |
|
1480 Print a list of which files have been opened, and whether they are open |
|
1481 for reading, writing, or both. For example, |
|
1482 |
|
1483 @example |
|
1484 @group |
|
1485 freport () |
|
1486 |
|
1487 @print{} number mode name |
|
1488 @print{} |
|
1489 @print{} 0 r stdin |
|
1490 @print{} 1 w stdout |
|
1491 @print{} 2 w stderr |
|
1492 @print{} 3 r myfile |
|
1493 @end group |
|
1494 @end example |
|
1495 @end deftypefn |
|
1496 |
|
1497 @node File Positioning, , EOF and Errors, C-Style I/O Functions |
|
1498 @subsection File Positioning |
|
1499 |
|
1500 Three functions are available for setting and determining the position of |
|
1501 the file pointer for a given file. |
|
1502 |
|
1503 @deftypefn {Built-in Function} {} ftell (@var{fid}) |
|
1504 Return the position of the file pointer as the number of characters |
|
1505 from the beginning of the file @var{fid}. |
|
1506 @end deftypefn |
|
1507 |
|
1508 @deftypefn {Built-in Function} {} fseek (@var{fid}, @var{offset}, @var{origin}) |
|
1509 Set the file pointer to any location within the file @var{fid}. The |
|
1510 pointer is positioned @var{offset} characters from the @var{origin}, |
|
1511 which may be one of the predefined variables @code{SEEK_CUR} (current |
|
1512 position), @code{SEEK_SET} (beginning), or @code{SEEK_END} (end of |
|
1513 file). If @var{origin} is omitted, @code{SEEK_SET} is assumed. The |
|
1514 offset must be zero, or a value returned by @code{ftell} (in which case |
|
1515 @var{origin} must be @code{SEEK_SET}. |
|
1516 @end deftypefn |
|
1517 |
|
1518 @defvr {Built-in Variable} SEEK_SET |
|
1519 @defvrx {Built-in Variable} SEEK_CUR |
|
1520 @defvrx {Built-in Variable} SEEK_END |
|
1521 These variables may be used as the optional third argument for the |
|
1522 function @code{fseek}. |
|
1523 @end defvr |
|
1524 |
|
1525 @deftypefn {Built-in Function} {} frewind (@var{fid}) |
|
1526 Move the file pointer to the beginning of the file @var{fid}, returning |
|
1527 1 for success, and 0 if an error was encountered. It is equivalent to |
|
1528 @code{fseek (@var{fid}, 0, SEEK_SET)}. |
|
1529 @end deftypefn |
|
1530 |
|
1531 The following example stores the current file position in the variable |
|
1532 @code{marker}, moves the pointer to the beginning of the file, reads |
|
1533 four characters, and then returns to the original position. |
|
1534 |
|
1535 @example |
|
1536 marker = ftell (myfile); |
|
1537 frewind (myfile); |
|
1538 fourch = fgets (myfile, 4); |
|
1539 fseek (myfile, marker, SEEK_SET); |
|
1540 @end example |
|
1541 |