Prints the argument with the string syntax. The
argument is, if no Unicode translation modifier is present, an
iolist(), a binary(), or an atom().
If the Unicode translation modifier (t) is in effect,
the argument is unicode:chardata(), meaning that
binaries are in UTF-8. The characters
are printed without quotes. The string is first truncated
by the specified precision and then padded and justified to the
specified field width. The default precision is the field width.
This format can be used for printing any object and
truncating the output so it fits a specified field:
1> io:fwrite("|~10w|~n", [{hey, hey, hey}]).
|**********|
ok
2> io:fwrite("|~10s|~n", [io_lib:write({hey, hey, hey})]).
|{hey,hey,h|
3> io:fwrite("|~-10.8s|~n", [io_lib:write({hey, hey, hey})]).
|{hey,hey |
ok
A list with integers > 255 is considered an error if the
Unicode translation modifier is not specified:
4> io:fwrite("~ts~n",[[1024]]).
\x{400}
ok
5> io:fwrite("~s~n",[[1024]]).
** exception error: bad argument
in function io:format/3
called as io:format(<0.53.0>,"~s~n",[[1024]])
Writes the data with standard syntax in the same way as
~w, but breaks terms whose printed representation
is longer than one line into many lines and indents each
line sensibly. Left-justification is not supported.
It also tries to detect flat lists of
printable characters and output these as strings.
For example:
1> T = [{attributes,[[{id,age,1.50000},{mode,explicit},
{typename,"INTEGER"}], [{id,cho},{mode,explicit},{typename,'Cho'}]]},
{typename,'Person'},{tag,{'PRIVATE',3}},{mode,implicit}].
...
2> io:fwrite("~w~n", [T]).
[{attributes,[[{id,age,1.5},{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
3> io:fwrite("~62p~n", [T]).
[{attributes,[[{id,age,1.5},
{mode,explicit},
{typename,"INTEGER"}],
[{id,cho},{mode,explicit},{typename,'Cho'}]]},
{typename,'Person'},
{tag,{'PRIVATE',3}},
{mode,implicit}]
ok
The 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
write/1 or
format/1,2,3.
For example, using T above:
4> io:fwrite("Here T = ~62p~n", [T]).
Here T = [{attributes,[[{id,age,1.5},
{mode,explicit},
{typename,"INTEGER"}],
[{id,cho},
{mode,explicit},
{typename,'Cho'}]]},
{typename,'Person'},
{tag,{'PRIVATE',3}},
{mode,implicit}]
ok
As from Erlang/OTP 21.0, a field width of value
0 can be used for specifying that a line is
infinitely long, which means that no line breaks
are inserted. For example:
5> io:fwrite("~0p~n", [lists:seq(1, 30)]).
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
ok
When the modifier l is specified, no detection of
printable character lists takes place, for example:
6> S = [{a,"a"}, {b, "b"}],
io:fwrite("~15p~n", [S]).
[{a,"a"},
{b,"b"}]
ok
7> io:fwrite("~15lp~n", [S]).
[{a,[97]},
{b,[98]}]
ok
The Unicode translation modifier t specifies how to treat
characters outside the Latin-1 range of codepoints, in
atoms, strings, and binaries. For example, printing an atom
containing a character > 255:
8> io:fwrite("~p~n",[list_to_atom([1024])]).
'\x{400}'
ok
9> io:fwrite("~tp~n",[list_to_atom([1024])]).
'Ѐ'
ok
By default, Erlang only detects lists of characters
in the Latin-1 range as strings, but the +pc unicode
flag can be used to change this (see
printable_range/0 for details). For example:
10> io:fwrite("~p~n",[[214]]).
"Ö"
ok
11> io:fwrite("~p~n",[[1024]]).
[1024]
ok
12> io:fwrite("~tp~n",[[1024]]).
[1024]
ok
but if Erlang was started with +pc unicode:
13> io:fwrite("~p~n",[[1024]]).
[1024]
ok
14> io:fwrite("~tp~n",[[1024]]).
"Ѐ"
ok
Similarly, binaries that look like UTF-8 encoded strings
are output with the binary string syntax if the t
modifier is specified:
15> io:fwrite("~p~n", [<<208,128>>]).
<<208,128>>
ok
16> io:fwrite("~tp~n", [<<208,128>>]).
<<"Ѐ"/utf8>>
ok
17> io:fwrite("~tp~n", [<<128,128>>]).
<<128,128>>
ok