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