annotate doc/interpreter/io.texi @ 3180:c17387059fd3

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