projog

3.87. writef(X,Y) - writes formatted text to the output stream.

The first argument is an atom representing the text to be output. The text can contain special character sequences which specify formatting and substitution rules.

Supported special character sequences are:

Sequence Action
\n Output a 'new line' character (ASCII code 10).
\l Same as \n.
\r Output a 'carriage return' character (ASCII code 13).
\t Output a tab character (ASCII code 9).
\\ Output the \ character.
\% Output the % character.
\\uNNNN Output the unicode character represented by the hex digits NNNN.
%t Output the next term - in same format as write/1.
%w Same as \t.
%q Same as \t.
%p Same as \t.
%d Output the next term - in same format as write_canonical/1.
%f Ignored (only included to support compatibility with other Prologs).
%s Output the elements contained in the list represented by the next term.
%n Output the character code of the next term.
%r Write the next term N times, where N is the value of the second term.
%Nc Write the next term centered in N columns.
%Nl Write the next term left-aligned in N columns.
%Nr Write the next term right-aligned in N columns.

writef(X) produces the same results as writef(X, []).

Examples

?- writef('%s%n %t%r', [[h,e,l,l,o], 44, world, !, 3]).
hello, world!!!
yes

?- writef('.%7l.\\n.%7l.\\n.%7l.\\n.%7l.\\n.%7l.', [a, abc, abcd, abcdefg, abcdefgh]).
.a      .
.abc    .
.abcd   .
.abcdefg.
.abcdefgh.

yes

?- writef('.%7r.\\n.%7r.\\n.%7r.\\n.%7r.\\n.%7r.', [a, abc, abcd, abcdefg, abcdefgh]).
.      a.
.    abc.
.   abcd.
.abcdefg.
.abcdefgh.

yes

?- writef('.%7c.\\n.%7c.\\n.%7c.\\n.%7c.\\n.%7c.', [a, abc, abcd, abcdefg, abcdefgh]).
.   a   .
.  abc  .
. abcd  .
.abcdefg.
.abcdefgh.

yes

?- writef('%w %d', [1+1, 1+1]).
1 + 1 +(1, 1)
yes

?- writef('\\%\\%%q\\\\\\\\\\r\\n\\u0048',[abc]).
%%abc\\
H

yes

Note: calling writef with only 1 argument is the same as calling it with an empty list for the second argument:

?- writef('\\u0048\\u0065\\u006C\\u006c\\u006F', []).
Hello
yes

?- writef('\\u0048\\u0065\\u006C\\u006c\\u006F').
Hello
yes