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