This module provides an interface to standard Erlang IO servers.
The output functions all return ok
if they are successful,
or exit if they are not.
In the following description, all functions have an optional
parameter IoDevice
. If included, it must be the pid of a
process which handles the IO protocols. Normally, it is the
IoDevice
returned by
file:open/2.
For a description of the IO protocols refer to Armstrong, Virding and Williams, 'Concurrent Programming in Erlang', Chapter 13, unfortunately now very outdated, but the general principles still apply.
io_device() as returned by file:open/2, a process handling IO protocols
put_chars([IoDevice,] IoData) -> ok
Types:
IoDevice = io_device()
IoData = iodata() -- see erlang(3)
Writes the characters of IoData
to the standard output
(IoDevice
).
Types:
IoDevice = io_device()
Writes new line to the standard output (IoDevice
).
get_chars([IoDevice,] Prompt, Count) -> string() | eof
Types:
IoDevice = io_device()
Prompt = atom() | string()
Count = int()
Reads Count
characters from standard input
(IoDevice
), prompting it with Prompt
. It
returns:
String
eof
get_line([IoDevice,] Prompt) -> string() | eof
Types:
IoDevice = io_device()
Prompt = atom() | string()
Reads a line from the standard input (IoDevice
),
prompting it with Prompt
. It returns:
String
eof
setopts([IoDevice,] Opts) -> ok | {error, Reason}
Types:
IoDevice = io_device()
Opts = [Opt]
Opt = binary | list
Reason = term()
Set options for standard input/output (IoDevice
).
Possible options are:
binary
get_chars/2,3
and get_line/1,2
return
binaries instead of lists of chars.list
get_chars/2,3
and get_line/1,2
return
lists of chars, which is the default.expand_fun
get_line/1,2
.{yes|no, string(), [string(), ...]}
. The
first element gives a beep if no
, otherwise the
expansion is silent, the second is a string that will be
entered at the cursor position, and the third is a list of
possible expansions. If this list is non-empty, the list
will be printed and the current input line will be written
once again.fun("") -> {yes, "quit", []}; (_) -> {no, "", ["quit"]} end
The |
Types:
IoDevice = io_device()
Term = term()
Writes the term Term
to the standard output
(IoDevice
).
read([IoDevice,] Prompt) -> Result
Types:
IoDevice = io_device()
Prompt = atom() | string()
Result = {ok, Term} | eof | {error, ErrorInfo}
Term = term()
ErrorInfo -- see section Error Information below
Reads a term Term
from the standard input
(IoDevice
), prompting it with Prompt
. It
returns:
{ok, Term}
eof
{error, ErrorInfo}
read(IoDevice, Prompt, StartLine) -> Result
Types:
IoDevice = io_device()
Prompt = atom() | string()
StartLine = int()
Result = {ok, Term, EndLine} | {eof, EndLine} |
{error, ErrorInfo, EndLine}
Term = term()
EndLine = int()
ErrorInfo -- see section Error Information below
Reads a term Term
from IoDevice
, prompting it
with Prompt
. Reading starts at line number
StartLine
. It returns:
{ok, Term, EndLine}
{eof, EndLine}
{error, ErrorInfo, EndLine}
fwrite(Format) ->
fwrite([IoDevice,] Format, Data) -> ok
format(Format) ->
format([IoDevice,] Format, Data) -> ok
Types:
IoDevice = io_device()
Format = atom() | string()
Data = [term()]
Writes the items in Data
([]
) on the standard
output (IoDevice
) in accordance with Format
.
Format
contains plain characters which are copied to
the output device, and control sequences for formatting, see
below. If Format
is an atom, it is first converted to
a list with the aid of atom_to_list/1
.
1> io:fwrite("Hello world!~n", []). Hello world! ok
The general format of a control sequence is ~F.P.PadC
.
The character C
determines the type of control sequence
to be used, F
and P
are optional numeric
arguments. If F
, P
, or Pad
is *
,
the next argument in Data
is used as the numeric value
of F
or P
.
F
is the field width
of the printed argument. A
negative value means that the argument will be left justified
within the field, otherwise it will be right justified. If no
field width is specified, the required print width will be
used. If the field width specified is too small, then the
whole field will be filled with *
characters.
P
is the precision
of the printed argument. A
default value is used if no precision is specified. The
interpretation of precision depends on the control sequences.
Unless otherwise specified, the argument within
is used
to determine print width.
Pad
is the padding character. This is the character
used to pad the printed representation of the argument so that
it conforms to the specified field width and precision. Only
one padding character can be specified and, whenever
applicable, it is used for both the field width and precision.
The default padding character is ' '
(space).
The following control sequences are available:
~
~
is written.c
2> io:fwrite("|~10.5c|~-10.5c|~5c|~n", [$a, $b, $c]). | aaaaa|aaaaa |ccccc| ok
f
[-]ddd.ddd
, where the precision is the number of
digits after the decimal point. The default precision is 6
and it cannot be less than 1.e
[-]d.ddde+-ddd
, where the precision is the number
of digits written. The default precision is 6 and it
cannot be less than 2.g
f
, if
it is >= 0.1 and < 10000.0. Otherwise, it is written
in the e
format. The precision is the number of
significant digits. It defaults to 6 and should not be
less than 2. If the absolute value of the float does not
allow it to be written in the f
format with the
desired number of significant digits, it is also written
in the e
format.s
string
syntax. The
argument is an I/O
list, a binary, or an atom. The characters
are printed without quotes. In this format, the printed
argument is truncated to the given precision and field
width.3> io:fwrite("|~10w|~n", [{hey, hey, hey}]). |**********| ok 4> io:fwrite("|~10s|~n", [io_lib:write({hey, hey, hey})]). |{hey,hey,h| ok
w
g
format.p
~w
, but breaks terms whose printed representation
is longer than one line into many lines and indents each
line sensibly. It also tries to detect lists of printable
characters and to output these as strings. For example:
5> T = [{attributes,[[{id,age,1.50000},{mode,explicit}, {typename,"INTEGER"}], [{id,cho},{mode,explicit},{typename,'Cho'}]]}, {typename,'Person'},{tag,{'PRIVATE',3}},{mode,implicit}]. ... 6> io:fwrite("~w~n", [T]). [{attributes,[[{id,age,1.50000},{mode,explicit},{typename, [73,78,84,69,71,69,82]}],[{id,cho},{mode,explicit},{typena me,'Cho'}]]},{typename,'Person'},{tag,{'PRIVATE',3}},{mode ,implicit}] ok 7> io:fwrite("~p~n", [T]). [{attributes,[[{id,age,1.50000}, {mode,explicit}, {typename,"INTEGER"}], [{id,cho},{mode,explicit},{typename,'Cho'}]]}, {typename,'Person'}, {tag,{'PRIVATE',3}}, {mode,implicit}] okThe field width specifies the maximum line length. It defaults to 80. The precision specifies the initial indentation of the term. It defaults to the number of characters printed on this line in the
same
call to
io:fwrite
or io:format
. For example, using
T
above:8> io:fwrite("Here T = ~p~n", [T]). Here T = [{attributes,[[{id,age,1.50000}, {mode,explicit}, {typename,"INTEGER"}], [{id,cho},{mode,explicit}, {typename,'Cho'}]]}, {typename,'Person'}, {tag,{'PRIVATE',3}}, {mode,implicit}] ok
W
~w
, but takes an
extra argument which is the maximum depth to which terms
are printed. Anything below this depth is replaced with
...
. For example, using T
above:9> io:fwrite("~W~n", [T,9]). [{attributes,[[{id,age,1.50000},{mode,explicit},{typename| ...}],[{id,cho},{mode|...},{...}]]},{typename,'Person'},{t ag,{'PRIVATE',3}},{mode,implicit}] okIf the maximum depth has been reached, then it is impossible to read in the resultant output. Also, the
|...
form in a tuple denotes that there are more
elements in the tuple but these are below the print depth.
P
~p
, but takes an
extra argument which is the maximum depth to which terms
are printed. Anything below this depth is replaced with
...
. For example:
10> io:fwrite("~P~n", [T,9]). [{attributes,[[{id,age,1.50000},{mode,explicit}, {typename|...}], [{id,cho},{mode|...},{...}]]}, {typename,'Person'}, {tag,{'PRIVATE',3}}, {mode,implicit}] ok
B
11> io:format("~.16B~n", [31]). 1F ok 12> io:format("~.2B~n", [-19]). -10011 ok 13> io:format("~.36B~n", [5*36+35]). 5Z ok
X
B
, but takes an extra argument that is a
prefix to insert before the number, but after the leading
dash, if any.14> io:format("~X~n", [31,"10#"]). 10#31 ok 15> io:format("~.16X~n", [-31,"0x"]). -0x1F ok
#
B
, but prints the number with an Erlang style
'#'-separated base prefix.16> io:format("~.10#~n", [31]). 10#31 ok 17> io:format("~.16#~n", [-31]). -16#1F ok
b
B
, but prints lowercase letters.x
X
, but prints lowercase letters.+
#
, but prints lowercase letters.n
i
Returns:
ok
If an error occurs, there is no output. For example:
18> io:fwrite("~s ~w ~i ~w ~c ~n",['abc def', 'abc def', {foo, 1},{foo, 1}, 65]). abc def 'abc def' {foo, 1} A ok 19> io:fwrite("~s", [65]). ** exited: {badarg,[{io,format,[<0.22.0>,"~s","A"]}, {erl_eval,do_apply,5}, {shell,exprs,6}, {shell,eval_loop,2}]} **
In this example, an attempt was made to output the single character '65' with the aid of the string formatting directive "~s".
The two functions fwrite
and format
are
identical. The old name format
has been retained for
backwards compatibility, while the new name fwrite
has
been added as a logical complement to fread
.
fread([IoDevice,] Prompt, Format) -> Result
Types:
IoDevice = io_device()
Prompt = atom() | string()
Format = string()
Result = {ok, Terms} | eof | {error, What}
Terms = [term()]
What = term()
Reads characters from the standard input (IoDevice
),
prompting it with Prompt
. Interprets the characters in
accordance with Format
. Format
contains control
sequences which directs the interpretation of the input.
Format
may contain:
~*FC
. The character *
is an optional return
suppression character. It provides a method to specify a
field which is to be omitted. F
is the field
width
of the input field and C
determines the
type of control sequence.~
~
is expected in the input.d
u
-
-1
. Sign
character '+' or none gives 1
. The field width
parameter is ignored. Leading white-space characters
are not skipped.#
"16#ffff"
) is expected.f
s
a
s
, but the resulting string is
converted into an atom.c
s
. All
characters are returned.l
{ok, Terms}
Terms
is the list
of successfully matched and read items.eof
{error, What}
What
gives a hint about the error.Examples:
20> io:fread('enter>', "~f~f~f"). enter>1.9 35.5e3 15.0 {ok,[1.90000,3.55000e+4,15.0000]} 21> io:fread('enter>', "~10f~d"). enter> 5.67899 {ok, [5.67800, 99]} 22> io:fread('enter>', ":~10s:~10c:"). enter>: alan : joe : {ok, ["alan", " joe "]}
scan_erl_exprs(Prompt) ->
scan_erl_exprs([IoDevice,] Prompt, StartLine) -> Result
Types:
IoDevice = io_device()
Prompt = atom() | string()
StartLine = int()
Result = {ok, Tokens, EndLine} | {eof, EndLine} |
{error, ErrorInfo, EndLine}
Tokens -- see erl_scan(3)
EndLine = int()
ErrorInfo -- see section Error Information below
Reads data from the standard input (IoDevice
),
prompting it with Prompt
. Reading starts at line number
StartLine
(1). The data is tokenized as if it were a
sequence of Erlang expressions until a final '.'
is
reached. This token is also returned. It returns:
{ok, Tokens, EndLine}
{eof, EndLine}
{error, ErrorInfo, EndLine}
Example:
23> io:scan_erl_exprs('enter>'). enter>abc(), "hey". {ok,[{atom,1,abc},{'(',1},{')',1},{',',1},{string,1,"hey"},{dot,1}],2} 24> io:scan_erl_exprs('enter>'). enter>1.0er. {error,{1,erl_scan,{illegal,float}},2}
scan_erl_form(Prompt) ->
scan_erl_form([IoDevice,] Prompt, StartLine) -> Result
Types:
IoDevice = io_device()
Prompt = atom() | string()
StartLine = int()
Result = {ok, Tokens, EndLine} | {eof, EndLine} |
{error, ErrorInfo, EndLine}
Tokens -- see erl_scan(3)
EndLine = int()
ErrorInfo -- see section Error Information below
Reads data from the standard input (IoDevice
),
prompting it with Prompt
. Starts reading at line number
StartLine
(1). The data is tokenized as if it were an
Erlang form - one of the valid Erlang expressions in an
Erlang source file - until a final '.'
is reached.
This last token is also returned. The return values are the
same as for scan_erl_exprs/1,2,3
above.
parse_erl_exprs(Prompt) ->
parse_erl_exprs([IoDevice,] Prompt, StartLine) -> Result
Types:
IoDevice = io_device()
Prompt = atom() | string()
StartLine = int()
Result = {ok, Expr_list, EndLine} | {eof, EndLine} |
{error, ErrorInfo, EndLine}
Expr_list -- see erl_parse(3)
EndLine = int()
ErrorInfo -- see section Error Information below
Reads data from the standard input (IoDevice
),
prompting it with Prompt
. Starts reading at line number
StartLine
(1). The data is tokenized and parsed as if
it were a sequence of Erlang expressions until a final '.' is
reached. It returns:
{ok, Expr_list, EndLine}
{eof, EndLine}
{error, ErrorInfo, EndLine}
Example:
25> io:parse_erl_exprs('enter>'). enter>abc(), "hey". {ok, [{call,1,{atom,1,abc},[]},{string,1,"hey"}],2} 26> io:parse_erl_exprs ('enter>'). enter>abc("hey". {error,{1,erl_parse,["syntax error before: ",["'.'"]]},2}
parse_erl_form(Prompt) ->
parse_erl_form([IoDevice,] Prompt, StartLine) -> Result
Types:
IoDevice = io_device()
Prompt = atom() | string()
StartLine = int()
Result = {ok, AbsForm, EndLine} | {eof, EndLine} |
{error, ErrorInfo, EndLine}
AbsForm -- see erl_parse(3)
EndLine = int()
ErrorInfo -- see section Error Information below
Reads data from the standard input (IoDevice
),
prompting it with Prompt
. Starts reading at line number
StartLine
(1). The data is tokenized and parsed as if
it were an Erlang form - one of the valid Erlang expressions
in an Erlang source file - until a final '.' is reached. It
returns:
{ok, AbsForm, EndLine}
{eof, EndLine}
{error, ErrorInfo, EndLine}
All Erlang processes have a default standard IO device. This
device is used when no IoDevice
argument is specified in
the above function calls. However, it is sometimes desirable to
use an explicit IoDevice
argument which refers to the
default IO device. This is the case with functions that can
access either a file or the default IO device. The atom
standard_io
has this special meaning. The following example
illustrates this:
27> io:read('enter>'). enter>foo. {ok,foo} 28> io:read(standard_io, 'enter>'). enter>bar. {ok,bar}
There is always a process registered under the name of
user
. This can be used for sending output to the user.
The ErrorInfo
mentioned above is the standard
ErrorInfo
structure which is returned from all IO modules.
It has the format:
{ErrorLine, Module, ErrorDescriptor}
A string which describes the error is obtained with the following call:
apply(Module, format_error, ErrorDescriptor)