/usr/lib/swi-prolog/doc/Manual/IO.html is in swi-prolog-nox 6.6.4-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>SWI-Prolog 7.1.10 Reference Manual: Section 4.16</title><link rel="home" href="index.html">
<link rel="contents" href="Contents.html">
<link rel="index" href="DocIndex.html">
<link rel="summary" href="summary.html">
<link rel="previous" href="examineprog.html">
<link rel="next" href="streamstat.html">
<style type="text/css">
/* Style sheet for SWI-Prolog latex2html
*/
dd.defbody
{ margin-bottom: 1em;
}
dt.pubdef, dt.multidef
{ color: #fff;
padding: 2px 10px 0px 10px;
margin-bottom: 5px;
font-size: 18px;
vertical-align: middle;
overflow: hidden;
}
dt.pubdef { background-color: #0c3d6e; }
dt.multidef { background-color: #ef9439; }
.bib dd
{ margin-bottom: 1em;
}
.bib dt
{ float: left;
margin-right: 1.3ex;
}
pre.code
{ margin-left: 1.5em;
margin-right: 1.5em;
border: 1px dotted;
padding-top: 5px;
padding-left: 5px;
padding-bottom: 5px;
background-color: #f8f8f8;
}
div.navigate
{ text-align: center;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
}
div.title
{ text-align: center;
padding-bottom: 1em;
font-size: 200%;
font-weight: bold;
}
div.author
{ text-align: center;
font-style: italic;
}
div.abstract
{ margin-top: 2em;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
margin-left: 10%; margin-right:10%;
}
div.abstract-title
{ text-align: center;
padding: 5px;
font-size: 120%;
font-weight: bold;
}
div.toc-h1
{ font-size: 200%;
font-weight: bold;
}
div.toc-h2
{ font-size: 120%;
font-weight: bold;
margin-left: 2em;
}
div.toc-h3
{ font-size: 100%;
font-weight: bold;
margin-left: 4em;
}
div.toc-h4
{ font-size: 100%;
margin-left: 6em;
}
span.sec-nr
{
}
span.sec-title
{
}
span.pred-ext
{ font-weight: bold;
}
span.pred-tag
{ float: right;
padding-top: 0.2em;
font-size: 80%;
font-style: italic;
color: #fff;
}
div.caption
{ width: 80%;
margin: auto;
text-align:center;
}
/* Footnotes */
.fn {
color: red;
font-size: 70%;
}
.fn-text, .fnp {
position: absolute;
top: auto;
left: 10%;
border: 1px solid #000;
box-shadow: 5px 5px 5px #888;
display: none;
background: #fff;
color: #000;
margin-top: 25px;
padding: 8px 12px;
font-size: larger;
}
sup:hover span.fn-text
{ display: block;
}
/* Lists */
dl.latex
{ margin-top: 1ex;
margin-bottom: 0.5ex;
}
dl.latex dl.latex dd.defbody
{ margin-bottom: 0.5ex;
}
/* PlDoc Tags */
dl.tags
{ font-size: 90%;
margin-left: 5ex;
margin-top: 1ex;
margin-bottom: 0.5ex;
}
dl.tags dt
{ margin-left: 0pt;
font-weight: bold;
}
dl.tags dd
{ margin-left: 3ex;
}
td.param
{ font-style: italic;
font-weight: bold;
}
/* Index */
dt.index-sep
{ font-weight: bold;
font-size: +1;
margin-top: 1ex;
}
/* Tables */
table.center
{ margin: auto;
}
table.latex
{ border-collapse:collapse;
}
table.latex tr
{ vertical-align: text-top;
}
table.latex td,th
{ padding: 2px 1em;
}
table.latex tr.hline td,th
{ border-top: 1px solid black;
}
table.frame-box
{ border: 2px solid black;
}
</style>
</head>
<body style="background:white">
<div class="navigate"><a class="nav" href="index.html"><img src="home.gif" alt="Home"></a>
<a class="nav" href="Contents.html"><img src="index.gif" alt="Contents"></a>
<a class="nav" href="DocIndex.html"><img src="yellow_pages.gif" alt="Index"></a>
<a class="nav" href="summary.html"><img src="info.gif" alt="Summary"></a>
<a class="nav" href="examineprog.html"><img src="prev.gif" alt="Previous"></a>
<a class="nav" href="streamstat.html"><img src="next.gif" alt="Next"></a>
</div>
<h2 id="sec:IO"><a id="sec:4.16"><span class="sec-nr">4.16</span> <span class="sec-title">Input
and output</span></a></h2>
<a id="sec:IO"></a>
<p>SWI-Prolog provides two different packages for input and output. The
native I/O system is based on the ISO standard predicates <a id="idx:open3:810"></a><a class="pred" href="IO.html#open/3">open/3</a>,
<a id="idx:close1:811"></a><a class="pred" href="IO.html#close/1">close/1</a>
and friends.<sup class="fn">60<span class="fn-text">Actually based on
Quintus Prolog, providing this interface before the ISO standard
existed.</span></sup> Being more widely portable and equipped with a
clearer and more robust specification, new code is encouraged to use
these predicates for manipulation of I/O streams.
<p><a class="sec" href="IO.html">Section 4.16.3</a> describes <a id="idx:tell1:812"></a><a class="pred" href="IO.html#tell/1">tell/1</a>, <a id="idx:see1:813"></a><a class="pred" href="IO.html#see/1">see/1</a>
and friends, providing I/O in the spirit of the traditional Edinburgh
standard. These predicates are layered on top of the ISO predicates.
Both packages are fully integrated; the user may switch freely between
them.
<p><h3 id="sec:streamalias"><a id="sec:4.16.1"><span class="sec-nr">4.16.1</span> <span class="sec-title">Predefined
stream aliases</span></a></h3>
<a id="sec:streamalias"></a>
<p>Each thread has five stream aliases: <code>user_input</code>,
<code>user_output</code>, <code>user_error</code>, <code>current_input</code>,
and
<code>current_output</code>. Newly created threads inherit these stream
aliases from theyr parent. The <code>user_input</code>, <code>user_output</code>
and <code>user_error</code> aliases of the <code>main</code> thread are
initially bound to the standard operating system I/O streams (<em>stdin</em>,
<em>stdout</em> and <em>stderr</em>, normally bound to the POSIX file
handles 0, 1 and 2). These aliases may be re-bound, for
example if standard I/O refers to a window such as in the <b>swipl-win.exe</b>
GUI executable for Windows. They can be re-bound by the user using
<a id="idx:setprologIO3:814"></a><a class="pred" href="IO.html#set_prolog_IO/3">set_prolog_IO/3</a>
and <a id="idx:setstream2:815"></a><a class="pred" href="IO.html#set_stream/2">set_stream/2</a>
by setting the alias of a stream (e.g,
<code>set_stream(S, alias(user_output))</code>). An example of rebinding
can be found in library <code>library(prolog_server)</code>, providing a <b>telnet</b>
service. The aliases <code>current_input</code> and <code>current_output</code>
define the source and destination for predicates that do not take a
stream argument (e.g., <a id="idx:read1:816"></a><a class="pred" href="termrw.html#read/1">read/1</a>, <a id="idx:write1:817"></a><a class="pred" href="termrw.html#write/1">write/1</a>, <a id="idx:getcode1:818"></a><a class="pred" href="chario.html#get_code/1">get_code/1</a>,
... ). Initially, these are bound to the same stream as <code>user_input</code>
and
<code>user_error</code>. They are re-bound by <a id="idx:see1:819"></a><a class="pred" href="IO.html#see/1">see/1</a>, <a id="idx:tell1:820"></a><a class="pred" href="IO.html#tell/1">tell/1</a>, <a id="idx:setinput1:821"></a><a class="pred" href="IO.html#set_input/1">set_input/1</a>
and
<a id="idx:setoutput1:822"></a><a class="pred" href="IO.html#set_output/1">set_output/1</a>.
The <code>current_output</code> stream is also temporary re-bound by <a id="idx:withoutputto2:823"></a><a class="pred" href="IO.html#with_output_to/2">with_output_to/2</a>
or <a id="idx:format3:824"></a><a class="pred" href="format.html#format/3">format/3</a>
using e.g.,
<code>format(atom(A), ...</code>. Note that code which explicitly writes
to the streams <code>user_output</code> and <code>user_error</code> will
not be redirected by <a id="idx:withoutputto2:825"></a><a class="pred" href="IO.html#with_output_to/2">with_output_to/2</a>.
<p><b>Compatibility</b>
<p>Note that the ISO standard only defines the <code>user_*</code>
streams. The `current' streams can be accessed using <a id="idx:currentinput1:826"></a><a class="pred" href="IO.html#current_input/1">current_input/1</a>
and
<a id="idx:currentoutput1:827"></a><a class="pred" href="IO.html#current_output/1">current_output/1</a>.
For example, an ISO compatible implementation of
<a id="idx:write1:828"></a><a class="pred" href="termrw.html#write/1">write/1</a>
is
<pre class="code">
write(Term) :- current_output(Out), write_term(Out, Term).
</pre>
<p>while SWI-Prolog additionally allows for
<pre class="code">
write(Term) :- write(current_output, Term).
</pre>
<p><h3 id="sec:isoIO"><a id="sec:4.16.2"><span class="sec-nr">4.16.2</span> <span class="sec-title">ISO
Input and Output Streams</span></a></h3>
<a id="sec:isoIO"></a>
<p>The predicates described in this section provide ISO compliant I/O,
where streams are explicitly created using the predicate <a id="idx:open3:829"></a><a class="pred" href="IO.html#open/3">open/3</a>.
The resulting stream identifier is then passed as a parameter to the
reading and writing predicates to specify the source or destination of
the data.
<p>This schema is not vulnerable to filename and stream ambiguities as
well as changes to the working directory. On the other hand, using the
notion of current-I/O simplifies reusability of code without the need to
pass arguments around. E.g., see <a id="idx:withoutputto2:830"></a><a class="pred" href="IO.html#with_output_to/2">with_output_to/2</a>.
<p>SWI-Prolog streams are, compatible with the ISO standard, either
input or output streams. To accommodate portability to other systems, a
pair of streams can be packed into a <em>stream-pair</em>. See
<a id="idx:streampair3:831"></a><a class="pred" href="IO.html#stream_pair/3">stream_pair/3</a>
for details.
<p>SWI-Prolog stream handles are unique symbols that have no syntactical
representation. They are written as <code>\bnfmeta{stream}(hex-number)</code>,
which is not valid input for <a id="idx:read1:832"></a><a class="pred" href="termrw.html#read/1">read/1</a>.
They are realised using a <em>blob</em> of type <code>stream</code> (see <a id="idx:blob2:833"></a><a class="pred" href="typetest.html#blob/2">blob/2</a>
and <a class="sec" href="foreigninclude.html">section 9.4.7</a>).
<dl class="latex">
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="open/4"><strong>open</strong>(<var>+SrcDest,
+Mode, -Stream, +Options</var>)</a></dt>
<dd class="defbody">
ISO compliant predicate to open a stream. <var>SrcDest</var> is either
an atom specifying a file, or a term `<code>pipe(<var>Command</var>)</code>',
like <a id="idx:see1:834"></a><a class="pred" href="IO.html#see/1">see/1</a>
and <a id="idx:tell1:835"></a><a class="pred" href="IO.html#tell/1">tell/1</a>. <var>Mode</var>
is one of <code>read</code>, <code>write</code>,
<code>append</code> or <code>update</code>. Mode <code>append</code>
opens the file for writing, positioning the file pointer at the end.
Mode <code>update</code> opens the file for writing, positioning the
file pointer at the beginning of the file without truncating the file. <var>Stream</var>
is either a variable, in which case it is bound to an integer
identifying the stream, or an atom, in which case this atom will be the
stream identifier.<sup class="fn">61<span class="fn-text">New code
should use the <code>alias(Alias)</code> option for compatibility with
the ISO standard.</span></sup> The <var>Options</var> list can contain
the following options:
<dl class="latex">
<dt><strong>type</strong>(<var>Type</var>)</dt>
<dd class="defbody">
Using type <code>text</code> (default), Prolog will write a text file in
an operating system compatible way. Using type <code>binary</code> the
bytes will be read or written without any translation. See also the
option <code>encoding</code>.</dd>
<dt><strong>alias</strong>(<var>Atom</var>)</dt>
<dd class="defbody">
Gives the stream a name. Below is an example. Be careful with this
option as stream names are global. See also <a id="idx:setstream2:836"></a><a class="pred" href="IO.html#set_stream/2">set_stream/2</a>.
<pre class="code">
?- open(data, read, Fd, [alias(input)]).
...,
read(input, Term),
...
</pre>
</dd>
<dt><strong>encoding</strong>(<var>Encoding</var>)</dt>
<dd class="defbody">
Define the encoding used for reading and writing text to this stream.
The default encoding for type <code>text</code> is derived from the
Prolog flag <a class="flag" href="flags.html#flag:encoding">encoding</a>.
For <code>binary</code> streams the default encoding is <code>octet</code>.
For details on encoding issues, see <a class="sec" href="widechars.html">section
2.18.1</a>.</dd>
<dt><strong>bom</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
Check for a BOM (<em>Byte Order Marker</em>) or write one. If omitted,
the default is <code>true</code> for mode <code>read</code> and
<code>false</code> for mode <code>write</code>. See also <a id="idx:streamproperty2:837"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
and especially <a class="sec" href="widechars.html">section 2.18.1.1</a>
for a discussion of this feature.</dd>
<dt><strong>eof_action</strong>(<var>Action</var>)</dt>
<dd class="defbody">
Defines what happens if the end of the input stream is reached. Action
<code>eof_code</code> makes <a id="idx:get01:838"></a><a class="pred" href="chario.html#get0/1">get0/1</a>
and friends return -1, and <a id="idx:read1:839"></a><a class="pred" href="termrw.html#read/1">read/1</a>
and friends return the atom <code>end_of_file</code>. Repetitive reading
keeps yielding the same result. Action <code>error</code> is like <code>eof_code</code>,
but repetitive reading will raise an error. With action <code>reset</code>,
Prolog will examine the file again and return more data if the file has
grown.</dd>
<dt><strong>buffer</strong>(<var>Buffering</var>)</dt>
<dd class="defbody">
Defines output buffering. The atom <code>full</code> (default) defines
full buffering, <code>line</code> buffering by line, and <code>false</code>
implies the stream is fully unbuffered. Smaller buffering is useful if
another process or the user is waiting for the output as it is being
produced. See also <a id="idx:flushoutput01:840"></a><span class="pred-ext">flush_output/[0,1]</span>.
This option is not an ISO option.</dd>
<dt><strong>close_on_abort</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
If <code>true</code> (default), the stream is closed on an abort (see
<a id="idx:abort0:841"></a><a class="pred" href="toplevel.html#abort/0">abort/0</a>).
If <code>false</code>, the stream is not closed. If it is an output
stream, however, it will be flushed. Useful for logfiles and if the
stream is associated to a process (using the <code>pipe/1</code>
construct).</dd>
<dt><strong>locale</strong>(<var>+Locale</var>)</dt>
<dd class="defbody">
Set the locale that is used by notably <a id="idx:format2:842"></a><a class="pred" href="format.html#format/2">format/2</a>
for output on this stream. See <a class="sec" href="locale.html">section
4.22</a>.</dd>
<dt><strong>lock</strong>(<var>LockingMode</var>)</dt>
<dd class="defbody">
Try to obtain a lock on the open file. Default is <code>none</code>,
which does not lock the file. The value <code>read</code> or <code>shared</code>
means other processes may read the file, but not write it. The value
<code>write</code> or <code>exclusive</code> means no other process may
read or write the file.
<p>Locks are acquired through the POSIX function fcntl() using the
command
<code>F_SETLKW</code>, which makes a blocked call wait for the lock to
be released. Please note that fcntl() locks are <em>advisory</em> and
therefore only other applications using the same advisory locks honour
your lock. As there are many issues around locking in Unix, especially
related to NFS (network file system), please study the fcntl() manual
page before trusting your locks!
<p>The <code>lock</code> option is a SWI-Prolog extension.</dd>
<dt><strong>wait</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
This option can be combined with the <code>lock</code> option. If
<code>false</code> (default <code>true</code>), the open call returns
immediately with an exception if the file is locked. The exception has
the format
<code>permission_error(lock, source_sink, SrcDest)</code>.
</dd>
</dl>
<p>The option <code>reposition</code> is not supported in SWI-Prolog.
All streams connected to a file may be repositioned.</dd>
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="open/3"><strong>open</strong>(<var>+SrcDest,
+Mode, ?Stream</var>)</a></dt>
<dd class="defbody">
Equivalent to <a id="idx:open4:843"></a><a class="pred" href="IO.html#open/4">open/4</a>
with an empty option list.</dd>
<dt class="pubdef"><a id="open_null_stream/1"><strong>open_null_stream</strong>(<var>?Stream</var>)</a></dt>
<dd class="defbody">
Open an output stream that produces no output. All counting functions
are enabled on such a stream. It can be used to discard output (like
Unix <code>/dev/null</code>) or exploit the counting properties. The
initial encoding of <var>Stream</var> is <code>utf8</code>, enabling
arbitrary Unicode output. The encoding can be changed to determine byte
counts of the output in a particular encoding or validate if output is
possible in a particular encoding. For example, the code below
determines the number of characters emitted when writing <var>Term</var>.
<pre class="code">
write_length(Term, Len) :-
open_null_stream(Out),
write(Out, Term),
character_count(Out, Len0),
close(Out),
Len = Len0.
</pre>
</dd>
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="close/1"><strong>close</strong>(<var>+Stream</var>)</a></dt>
<dd class="defbody">
Close the specified stream. If <var>Stream</var> is not open, an
existence error is raised. However, closing a stream multiple times may
crash Prolog. This is particularly true for multithreaded applications.
<p>If the closed stream is the current input or output stream, the
terminal is made the current input or output.</dd>
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="close/2"><strong>close</strong>(<var>+Stream,
+Options</var>)</a></dt>
<dd class="defbody">
Provides <code>close(Stream, [force(true)])</code> as the only option.
Called this way, any resource errors (such as write errors while
flushing the output buffer) are ignored.</dd>
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="stream_property/2"><strong>stream_property</strong>(<var>?Stream,
?StreamProperty</var>)</a></dt>
<dd class="defbody">
ISO compatible predicate for querying the status of open I/O streams.
<var>StreamProperty</var> is one of:
<dl class="latex">
<dt><strong>alias</strong>(<var>Atom</var>)</dt>
<dd class="defbody">
If <var>Atom</var> is bound, test if the stream has the specified alias.
Otherwise unify <var>Atom</var> with the first alias of the stream.<sup class="fn">bug<span class="fn-text">Backtracking
does not give other aliases.</span></sup></dd>
<dt><strong>buffer</strong>(<var>Buffering</var>)</dt>
<dd class="defbody">
SWI-Prolog extension to query the buffering mode of this stream.
<var>Buffering</var> is one of <code>full</code>, <code>line</code> or <code>false</code>.
See also <a id="idx:open4:844"></a><a class="pred" href="IO.html#open/4">open/4</a>.</dd>
<dt><strong>buffer_size</strong>(<var>Integer</var>)</dt>
<dd class="defbody">
SWI-Prolog extension to query the size of the I/O buffer associated to a
stream in bytes. Fails if the stream is not buffered.</dd>
<dt><strong>bom</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
If present and <code>true</code>, a BOM (<em>Byte Order Mark</em>) was
detected while opening the file for reading, or a BOM was written while
opening the stream. See <a class="sec" href="widechars.html">section
2.18.1.1</a> for details.</dd>
<dt><strong>close_on_abort</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
Determine whether or not <a id="idx:abort0:845"></a><a class="pred" href="toplevel.html#abort/0">abort/0</a>
closes the stream. By default streams are closed.</dd>
<dt><strong>close_on_exec</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
Determine whether or not the stream is closed when executing a new
process (exec() in Unix, CreateProcess() in Windows). Default is to
close streams. This maps to fcntl() <code>F_SETFD</code> using the flag
<code>FD_CLOEXEC</code> on Unix and (negated) <code>HANDLE_FLAG_INHERIT</code>
on Windows.</dd>
<dt><strong>encoding</strong>(<var>Encoding</var>)</dt>
<dd class="defbody">
Query the encoding used for text. See <a class="sec" href="widechars.html">section
2.18.1</a> for an overview of wide character and encoding issues in
SWI-Prolog.</dd>
<dt><strong>end_of_stream</strong>(<var>E</var>)</dt>
<dd class="defbody">
If <var>Stream</var> is an input stream, unify <var>E</var> with one of
the atoms <code>not</code>, <code>at</code> or <code>past</code>. See
also
<a id="idx:atendofstream01:846"></a><span class="pred-ext">at_end_of_stream/[0,1]</span>.</dd>
<dt><strong>eof_action</strong>(<var>A</var>)</dt>
<dd class="defbody">
Unify <var>A</var> with one of <code>eof_code</code>, <code>reset</code>
or
<code>error</code>. See <a id="idx:open4:847"></a><a class="pred" href="IO.html#open/4">open/4</a>
for details.</dd>
<dt><strong>file_name</strong>(<var>Atom</var>)</dt>
<dd class="defbody">
If <var>Stream</var> is associated to a file, unify <var>Atom</var> to
the name of this file.</dd>
<dt><strong>file_no</strong>(<var>Integer</var>)</dt>
<dd class="defbody">
If the stream is associated with a POSIX file descriptor, unify
<var>Integer</var> with the descriptor number. SWI-Prolog extension used
primarily for integration with foreign code. See also Sfileno() from
<code>SWI-Stream.h</code>.</dd>
<dt><strong>input</strong></dt>
<dd class="defbody">
True if <var>Stream</var> has mode <code>read</code>.</dd>
<dt><strong>locale</strong>(<var>Locale</var>)</dt>
<dd class="defbody">
True when <var>Locale</var> is the current locale associated with the
stream. See <a class="sec" href="locale.html">section 4.22</a>.</dd>
<dt><strong>mode</strong>(<var>IOMode</var>)</dt>
<dd class="defbody">
Unify <var>IOMode</var> to the mode given to <a id="idx:open4:848"></a><a class="pred" href="IO.html#open/4">open/4</a>
for opening the stream. Values are: <code>read</code>, <code>write</code>, <code>append</code>
and the SWI-Prolog extension <code>update</code>.</dd>
<dt><strong>newline</strong>(<var>NewlineMode</var>)</dt>
<dd class="defbody">
One of <code>posix</code> or <code>dos</code>. If <code>dos</code>, text
streams will emit <code>\r\n</code> for <code>\n</code> and discard <code>\r</code>
from input streams. Default depends on the operating system.</dd>
<dt><strong>nlink</strong>(<var>-Count</var>)</dt>
<dd class="defbody">
Number of hard links to the file. This expresses the number of `names'
the file has. Not supported on all operating systems and the value might
be bogus. See the documentation of fstat() for your OS and the value
<code>st_nlink</code>.</dd>
<dt><strong>output</strong></dt>
<dd class="defbody">
True if <var>Stream</var> has mode <code>write</code>, <code>append</code>
or
<code>update</code>.</dd>
<dt><strong>position</strong>(<var>Pos</var>)</dt>
<dd class="defbody">
Unify <var>Pos</var> with the current stream position. A stream position
is an opaque term whose fields can be extracted using
<a id="idx:streampositiondata3:849"></a><a class="pred" href="IO.html#stream_position_data/3">stream_position_data/3</a>.
See also <a id="idx:setstreamposition2:850"></a><a class="pred" href="IO.html#set_stream_position/2">set_stream_position/2</a>.</dd>
<dt><strong>reposition</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
Unify <var>Bool</var> with <var>true</var> if the position of the stream
can be set (see <a id="idx:seek4:851"></a><a class="pred" href="IO.html#seek/4">seek/4</a>).
It is assumed the position can be set if the stream has a <em>seek-function</em>
and is not based on a POSIX file descriptor that is not associated to a
regular file.</dd>
<dt><strong>representation_errors</strong>(<var>Mode</var>)</dt>
<dd class="defbody">
Determines behaviour of character output if the stream cannot represent
a character. For example, an ISO Latin-1 stream cannot represent
Cyrillic characters. The behaviour is one of <code>error</code> (throw
an I/O error exception), <code>prolog</code> (write <code>\...\</code>
escape code) or <code>xml</code> (write <code>&#...;</code> XML
character entity). The initial mode is <code>prolog</code> for the user
streams and
<code>error</code> for all other streams. See also <a class="sec" href="widechars.html">section
2.18.1</a> and <a id="idx:setstream2:852"></a><a class="pred" href="IO.html#set_stream/2">set_stream/2</a>.</dd>
<dt><strong>timeout</strong>(<var>-Time</var>)</dt>
<dd class="defbody">
<var>Time</var> is the timeout currently associated with the stream. See
<a id="idx:setstream2:853"></a><a class="pred" href="IO.html#set_stream/2">set_stream/2</a>
with the same option. If no timeout is specified,
<var>Time</var> is unified to the atom <code>infinite</code>.</dd>
<dt><strong>type</strong>(<var>Type</var>)</dt>
<dd class="defbody">
Unify <var>Type</var> with <code>text</code> or <code>binary</code>.</dd>
<dt><strong>tty</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
This property is reported with <var>Bool</var> equal to <code>true</code>
if the stream is associated with a terminal. See also <a id="idx:setstream2:854"></a><a class="pred" href="IO.html#set_stream/2">set_stream/2</a>.
</dd>
</dl>
</dd>
<dt class="pubdef"><a id="current_stream/3"><strong>current_stream</strong>(<var>?Object,
?Mode, ?Stream</var>)</a></dt>
<dd class="defbody">
The predicate <a id="idx:currentstream3:855"></a><a class="pred" href="IO.html#current_stream/3">current_stream/3</a>
is used to access the status of a stream as well as to generate all open
streams. <var>Object</var> is the name of the file opened if the stream
refers to an open file, an integer file descriptor if the stream
encapsulates an operating system stream, or the atom <code>[]</code> if
the stream refers to some other object.
<var>Mode</var> is one of <code>read</code> or <code>write</code>.</dd>
<dt class="pubdef"><a id="is_stream/1"><strong>is_stream</strong>(<var>+Term</var>)</a></dt>
<dd class="defbody">
True if <var>Term</var> is a stream name or valid stream handle. This
predicate realises a safe test for the existence of a stream alias or
handle.</dd>
<dt class="pubdef"><a id="stream_pair/3"><strong>stream_pair</strong>(<var>?StreamPair,
?Read, ?Write</var>)</a></dt>
<dd class="defbody">
This predicate can be used in mode (-,+,+) to create a
<em>stream-pair</em> from an input stream and an output stream.
Stream-pairs can be used by all I/O operations on streams, where the
operation selects the appropriate member of the pair. The predicate
<a id="idx:close1:856"></a><a class="pred" href="IO.html#close/1">close/1</a>
closes both streams of the pair. Mode (+,-,-) can be used to get access
to the underlying streams.</dd>
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="set_stream_position/2"><strong>set_stream_position</strong>(<var>+Stream,
+Pos</var>)</a></dt>
<dd class="defbody">
Set the current position of <var>Stream</var> to <var>Pos</var>. <var>Pos</var>
is a term as returned by <a id="idx:streamproperty2:857"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
using the <code>position(Pos)</code> property. See also <a id="idx:seek4:858"></a><a class="pred" href="IO.html#seek/4">seek/4</a>.</dd>
<dt class="pubdef"><a id="stream_position_data/3"><strong>stream_position_data</strong>(<var>?Field,
+Pos, -Data</var>)</a></dt>
<dd class="defbody">
Extracts information from the opaque stream position term as returned by <a id="idx:streamproperty2:859"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
requesting the <code>position(Pos)</code> property.
<var>Field</var> is one of <code>line_count</code>, <code>line_position</code>,
<code>char_count</code> or <code>byte_count</code>. See also <a id="idx:linecount2:860"></a><a class="pred" href="streamstat.html#line_count/2">line_count/2</a>,
<a id="idx:lineposition2:861"></a><a class="pred" href="streamstat.html#line_position/2">line_position/2</a>, <a id="idx:charactercount2:862"></a><a class="pred" href="streamstat.html#character_count/2">character_count/2</a>
and <a id="idx:bytecount2:863"></a><a class="pred" href="streamstat.html#byte_count/2">byte_count/2</a>.<sup class="fn">62<span class="fn-text">Introduced
in version 5.6.4 after extending the position term with a byte count.
Compatible with SICStus Prolog.</span></sup></dd>
<dt class="pubdef"><a id="seek/4"><strong>seek</strong>(<var>+Stream,
+Offset, +Method, -NewLocation</var>)</a></dt>
<dd class="defbody">
Reposition the current point of the given <var>Stream</var>. <var>Method</var>
is one of <code>bof</code>, <code>current</code> or <code>eof</code>,
indicating positioning relative to the start, current point or end of
the underlying object. <var>NewLocation</var> is unified with the new
offset, relative to the start of the stream.
<p>Positions are counted in `units'. A unit is 1 byte, except for text
files using 2-byte Unicode encoding (2 bytes) or <em>wchar</em> encoding
(sizeof(wchar_t)). The latter guarantees comfortable interaction with
wide-character text objects. Otherwise, the use of
<a id="idx:seek4:864"></a><a class="pred" href="IO.html#seek/4">seek/4</a>
on non-binary files (see <a id="idx:open4:865"></a><a class="pred" href="IO.html#open/4">open/4</a>)
is of limited use, especially when using multi-byte text encodings (e.g. UTF-8)
or multi-byte newline files (e.g. DOS/Windows). On text files,
SWI-Prolog offers reliable backup to an old position using <a id="idx:streamproperty2:866"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
and
<a id="idx:setstreamposition2:867"></a><a class="pred" href="IO.html#set_stream_position/2">set_stream_position/2</a>.
Skipping <var>N</var> character codes is achieved calling
<a id="idx:getcode2:868"></a><a class="pred" href="chario.html#get_code/2">get_code/2</a> <var>N</var>
times or using <a id="idx:copystreamdata3:869"></a><a class="pred" href="chario.html#copy_stream_data/3">copy_stream_data/3</a>,
directing the output to a null stream (see <a id="idx:opennullstream1:870"></a><a class="pred" href="IO.html#open_null_stream/1">open_null_stream/1</a>).
If the seek modifies the current location, the line number and character
position in the line are set to 0.
<p>If the stream cannot be repositioned, a <code>permission_error</code>
is raised. If applying the offset would result in a file position less
than zero, a <code>domain_error</code> is raised. Behaviour when seeking
to positions beyond the size of the underlying object depend on the
object and possibly the operating system. The predicate <a id="idx:seek4:871"></a><a class="pred" href="IO.html#seek/4">seek/4</a>
is compatible with Quintus Prolog, though the error conditions and
signalling is ISO compliant. See also <a id="idx:streamproperty2:872"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
and <a id="idx:setstreamposition2:873"></a><a class="pred" href="IO.html#set_stream_position/2">set_stream_position/2</a>.</dd>
<dt class="pubdef"><a id="set_stream/2"><strong>set_stream</strong>(<var>+Stream,
+Attribute</var>)</a></dt>
<dd class="defbody">
Modify an attribute of an existing stream. <var>Attribute</var>
specifies the stream property to set. If stream is a <em>pair</em> (see <a id="idx:streampair3:874"></a><a class="pred" href="IO.html#stream_pair/3">stream_pair/3</a>)
both streams are modified, unless the property is only meaningful on one
of the streams or setting both is not meaningful. In particular,
<code>eof_action</code> only applies to the <em>read</em> stream,
<code>representation_errors</code> only applies to the <em>write</em>
stream and trying to set <code>alias</code> or <code>line_position</code>
on a pair results in a <code>permission_error</code> exception. See also
<a id="idx:streamproperty2:875"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
and <a id="idx:open4:876"></a><a class="pred" href="IO.html#open/4">open/4</a>.
<dl class="latex">
<dt><strong>alias</strong>(<var>AliasName</var>)</dt>
<dd class="defbody">
Set the alias of an already created stream. If <var>AliasName</var> is
the name of one of the standard streams, this stream is rebound. Thus, <code>set_stream(S,
current_input)</code> is the same as <a id="idx:setinput1:877"></a><a class="pred" href="IO.html#set_input/1">set_input/1</a>,
and by setting the alias of a stream to <code>user_input</code>, etc.,
all user terminal input is read from this stream. See also <a id="idx:interactor0:878"></a><a class="pred" href="thutil.html#interactor/0">interactor/0</a>.</dd>
<dt><strong>buffer</strong>(<var>Buffering</var>)</dt>
<dd class="defbody">
Set the buffering mode of an already created stream. Buffering is one of <code>full</code>, <code>line</code>
or <code>false</code>.</dd>
<dt><strong>buffer_size</strong>(<var>+Size</var>)</dt>
<dd class="defbody">
Set the size of the I/O buffer of the underlying stream to <var>Size</var>
bytes.</dd>
<dt><strong>close_on_abort</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
Determine whether or not the stream is closed by <a id="idx:abort0:879"></a><a class="pred" href="toplevel.html#abort/0">abort/0</a>.
By default, streams are closed.</dd>
<dt><strong>close_on_exec</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
Set the <code>close_on_exec</code> property. See <a id="idx:streamproperty2:880"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>.</dd>
<dt><strong>encoding</strong>(<var>Atom</var>)</dt>
<dd class="defbody">
Defines the mapping between bytes and character codes used for the
stream. See <a class="sec" href="widechars.html">section 2.18.1</a> for
supported encodings.</dd>
<dt><strong>eof_action</strong>(<var>Action</var>)</dt>
<dd class="defbody">
Set end-of-file handling to one of <code>eof_code</code>, <code>reset</code>
or
<code>error</code>.</dd>
<dt><strong>file_name</strong>(<var>FileName</var>)</dt>
<dd class="defbody">
Set the filename associated to this stream. This call can be used to set
the file for error locations if <var>Stream</var> corresponds to
<var>FileName</var> and is not obtained by opening the file directly
but, for example, through a network service.</dd>
<dt><strong>line_position</strong>(<var>LinePos</var>)</dt>
<dd class="defbody">
Set the line position attribute of the stream. This feature is intended
to correct position management of the stream after sending a terminal
escape sequence (e.g., setting ANSI character attributes). Setting this
attribute raises a permission error if the stream does not record
positions. See <a id="idx:lineposition2:881"></a><a class="pred" href="streamstat.html#line_position/2">line_position/2</a>
and <a id="idx:streamproperty2:882"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
(property <code>position</code>).</dd>
<dt><strong>locale</strong>(<var>+Locale</var>)</dt>
<dd class="defbody">
Change the locale of the stream. See <a class="sec" href="locale.html">section
4.22</a>.</dd>
<dt><strong>newline</strong>(<var>NewlineMode</var>)</dt>
<dd class="defbody">
Set input or output translation for newlines. See corresponding
<a id="idx:streamproperty2:883"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
for details. In addition to the detected modes, an input stream can be
set in mode <code>detect</code>. It will be set to <code>dos</code> if a <code>\r</code>
character was removed.</dd>
<dt><strong>timeout</strong>(<var>Seconds</var>)</dt>
<dd class="defbody">
This option can be used to make streams generate an exception if it
takes longer than <var>Seconds</var> before any new data arrives at the
stream. The value <var>infinite</var> (default) makes the stream block
indefinitely. Like <a id="idx:waitforinput3:884"></a><a class="pred" href="streamstat.html#wait_for_input/3">wait_for_input/3</a>,
this call only applies to streams that support the select() system call.
For further information about timeout handling, see <a id="idx:waitforinput3:885"></a><a class="pred" href="streamstat.html#wait_for_input/3">wait_for_input/3</a>.
The exception is of the form
<blockquote>
<code>error(<code>timeout_error(read, Stream)</code>, _)</code>
</blockquote>
</dd>
<dt><strong>type</strong>(<var>Type</var>)</dt>
<dd class="defbody">
Set the type of the stream to one of <code>text</code> or <code>binary</code>.
See also <a id="idx:open4:886"></a><a class="pred" href="IO.html#open/4">open/4</a>
and the <code>encoding</code> property of streams. Switching to <code>binary</code>
sets the encoding to <code>octet</code>. Switching to
<code>text</code> sets the encoding to the default text encoding.</dd>
<dt><strong>record_position</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
Do/do not record the line count and line position (see <a id="idx:linecount2:887"></a><a class="pred" href="streamstat.html#line_count/2">line_count/2</a>
and <a id="idx:lineposition2:888"></a><a class="pred" href="streamstat.html#line_position/2">line_position/2</a>).</dd>
<dt><strong>representation_errors</strong>(<var>Mode</var>)</dt>
<dd class="defbody">
Change the behaviour when writing characters to the stream that cannot
be represented by the encoding. See also <a id="idx:streamproperty2:889"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>
and
<a class="sec" href="widechars.html">section 2.18.1</a>.</dd>
<dt><strong>tty</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
Modify whether Prolog thinks there is a terminal (i.e. human
interaction) connected to this stream. On Unix systems the initial value
comes from isatty(). On Windows, the initial user streams are supposed
to be associated to a terminal. See also <a id="idx:streamproperty2:890"></a><a class="pred" href="IO.html#stream_property/2">stream_property/2</a>.
</dd>
</dl>
</dd>
<dt class="pubdef"><a id="set_prolog_IO/3"><strong>set_prolog_IO</strong>(<var>+In,
+Out, +Error</var>)</a></dt>
<dd class="defbody">
Prepare the given streams for interactive behaviour normally associated
to the terminal. <var>In</var> becomes the <code>user_input</code> and
<code>current_input</code> of the calling thread. <var>Out</var> becomes
<code>user_output</code> and <code>current_output</code>. If <var>Error</var>
equals
<var>Out</var> an unbuffered stream is associated to the same
destination and linked to <code>user_error</code>. Otherwise <var>Error</var>
is used for
<code>user_error</code>. Output buffering for <var>Out</var> is set to
<code>line</code> and buffering on <var>Error</var> is disabled. See
also <a id="idx:prolog0:891"></a><a class="pred" href="toplevel.html#prolog/0">prolog/0</a>
and <a id="idx:setstream2:892"></a><a class="pred" href="IO.html#set_stream/2">set_stream/2</a>.
The <em>clib</em> package provides the library <code>library(prolog_server)</code>,
creating a TCP/IP server for creating an interactive session to Prolog.
</dd>
</dl>
<p><h3 id="sec:edinburghIO"><a id="sec:4.16.3"><span class="sec-nr">4.16.3</span> <span class="sec-title">Edinburgh-style
I/O</span></a></h3>
<a id="sec:edinburghIO"></a>
<p>The package for implicit input and output destinations is (almost)
compatible with Edinburgh DEC-10 and C-Prolog. The reading and writing
predicates refer to, resp., the <em>current</em> input and output
streams. Initially these streams are connected to the terminal. The
current output stream is changed using <a id="idx:tell1:893"></a><a class="pred" href="IO.html#tell/1">tell/1</a>
or <a id="idx:append1:894"></a><a class="pred" href="IO.html#append/1">append/1</a>.
The current input stream is changed using <a id="idx:see1:895"></a><a class="pred" href="IO.html#see/1">see/1</a>.
The stream's current value can be obtained using <a id="idx:telling1:896"></a><a class="pred" href="IO.html#telling/1">telling/1</a>
for output and <a id="idx:seeing1:897"></a><a class="pred" href="IO.html#seeing/1">seeing/1</a>
for input.
<p>Source and destination are either a file, <code>user</code>, or a
term `pipe(<var>Command</var>)'. The reserved stream name <code>user</code>
refers to the terminal.<sup class="fn">63<span class="fn-text">The ISO
I/O layer uses <code>user_input</code>, <code>user_output</code> and <code>user_error</code>.</span></sup>
In the predicate descriptions below we will call the source/destination
argument `<var>SrcDest</var>'. Below are some examples of
source/destination specifications.
<p><table class="latex frame-void center">
<tr><td><code>?- see(data).</code> </td><td>% Start reading from file
`data'. </td></tr>
<tr><td><code>?- tell(user).</code> </td><td>% Start writing to the
terminal. </td></tr>
<tr><td><code>?- tell(pipe(lpr)).</code> </td><td>% Start writing to the
printer.</td></tr>
</table>
<p>Another example of using the <code>pipe/1</code> construct is shown
below.<sup class="fn">64<span class="fn-text">As of version 5.3.15, the
pipe construct is supported in the MS-Windows version, both for <b>swipl.exe</b>
and <b>swipl-win.exe</b>. The implementation uses code from the LUA
programming language (<a class="url" href="http://www.lua.org">http://www.lua.org</a>).</span></sup>
Note that the <code>pipe/1</code> construct is not part of Prolog's
standard I/O repertoire.
<pre class="code">
getwd(Wd) :-
seeing(Old), see(pipe(pwd)),
collect_wd(String),
seen, see(Old),
atom_codes(Wd, String).
collect_wd([C|R]) :-
get0(C), C \== -1, !,
collect_wd(R).
collect_wd([]).
</pre>
<p>The effect of <a id="idx:tell1:898"></a><a class="pred" href="IO.html#tell/1">tell/1</a>
is not undone on backtracking, and since the stream handle is not
specified explicitly in further I/O operations when using
Edinburgh-style I/O, you may write to unintended streams more easily
than when using ISO compliant I/O. For example, the following query
writes both "a" and "b" into the file `out' :
<pre class="code">
?- (tell(out), write(a), false ; write(b)), told.
</pre>
<h4>Compatibility notes</h4>
<p>Unlike Edinburgh Prolog systems, <a id="idx:telling1:899"></a><a class="pred" href="IO.html#telling/1">telling/1</a>
and <a id="idx:seeing1:900"></a><a class="pred" href="IO.html#seeing/1">seeing/1</a>
do not return the filename of the current input/output but rather the
stream identifier, to ensure the design pattern below works under all
circumstances:<sup class="fn">65<span class="fn-text">Filenames can be
ambiguous and SWI-Prolog streams can refer to much more than just files.</span></sup>
<pre class="code">
...,
telling(Old), tell(x),
...,
told, tell(Old),
...,
</pre>
<p>The predicates <a id="idx:tell1:901"></a><a class="pred" href="IO.html#tell/1">tell/1</a>
and <a id="idx:see1:902"></a><a class="pred" href="IO.html#see/1">see/1</a>
first check for <code>user</code>, the
<code>pipe(command)</code> and a stream handle. Otherwise, if the
argument is an atom it is first compared to open streams associated to a
file with <em>exactly</em> the same name. If such a stream exists,
created using
<a id="idx:tell1:903"></a><a class="pred" href="IO.html#tell/1">tell/1</a>
or <a id="idx:see1:904"></a><a class="pred" href="IO.html#see/1">see/1</a>,
output (input) is switched to the open stream. Otherwise a file with the
specified name is opened.
<p>The behaviour is compatible with Edinburgh Prolog. This is not
without problems. Changing directory, non-file streams, and multiple
names referring to the same file easily lead to unexpected behaviour.
New code, especially when managing multiple I/O channels, should
consider using the ISO I/O predicates defined in <a class="sec" href="IO.html">section
4.16.2</a>.
<dl class="latex">
<dt class="pubdef"><a id="see/1"><strong>see</strong>(<var>+SrcDest</var>)</a></dt>
<dd class="defbody">
Open <var>SrcDest</var> for reading and make it the current input (see
<a id="idx:setinput1:905"></a><a class="pred" href="IO.html#set_input/1">set_input/1</a>).
If <var>SrcDest</var> is a stream handle, just make this stream the
current input. See the introduction of <a class="sec" href="IO.html">section
4.16.3</a> for details.</dd>
<dt class="pubdef"><a id="tell/1"><strong>tell</strong>(<var>+SrcDest</var>)</a></dt>
<dd class="defbody">
Open <var>SrcDest</var> for writing and make it the current output (see
<a id="idx:setoutput1:906"></a><a class="pred" href="IO.html#set_output/1">set_output/1</a>).
If <var>SrcDest</var> is a stream handle, just make this stream the
current output. See the introduction of <a class="sec" href="IO.html">section
4.16.3</a> for details.</dd>
<dt class="pubdef"><a id="append/1"><strong>append</strong>(<var>+File</var>)</a></dt>
<dd class="defbody">
Similar to <a id="idx:tell1:907"></a><a class="pred" href="IO.html#tell/1">tell/1</a>,
but positions the file pointer at the end of <var>File</var> rather than
truncating an existing file. The pipe construct is not accepted by this
predicate.</dd>
<dt class="pubdef"><a id="seeing/1"><strong>seeing</strong>(<var>?SrcDest</var>)</a></dt>
<dd class="defbody">
Same as <a id="idx:currentinput1:908"></a><a class="pred" href="IO.html#current_input/1">current_input/1</a>,
except that <code>user</code> is returned if the current input is the
stream <code>user_input</code> to improve compatibility with traditional
Edinburgh I/O. See the introduction of
<a class="sec" href="IO.html">section 4.16.3</a> for details.</dd>
<dt class="pubdef"><a id="telling/1"><strong>telling</strong>(<var>?SrcDest</var>)</a></dt>
<dd class="defbody">
Same as <a id="idx:currentoutput1:909"></a><a class="pred" href="IO.html#current_output/1">current_output/1</a>,
except that <code>user</code> is returned if the current output is the
stream <code>user_output</code> to improve compatibility with
traditional Edinburgh I/O. See the introduction of
<a class="sec" href="IO.html">section 4.16.3</a> for details.</dd>
<dt class="pubdef"><a id="seen/0"><strong>seen</strong></a></dt>
<dd class="defbody">
Close the current input stream. The new input stream becomes
<code>user_input</code>.</dd>
<dt class="pubdef"><a id="told/0"><strong>told</strong></a></dt>
<dd class="defbody">
Close the current output stream. The new output stream becomes
<code>user_output</code>.
</dd>
</dl>
<p><h3 id="sec:edinburgh-iso-io"><a id="sec:4.16.4"><span class="sec-nr">4.16.4</span> <span class="sec-title">Switching
between Edinburgh and ISO I/O</span></a></h3>
<a id="sec:edinburgh-iso-io"></a>
<p>The predicates below can be used for switching between the implicit
and the explicit stream-based I/O predicates.
<dl class="latex">
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="set_input/1"><strong>set_input</strong>(<var>+Stream</var>)</a></dt>
<dd class="defbody">
Set the current input stream to become <var>Stream</var>. Thus, <code>open(file,
read, Stream), set_input(Stream)</code> is equivalent to <code>see(file)</code>.
</dd>
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="set_output/1"><strong>set_output</strong>(<var>+Stream</var>)</a></dt>
<dd class="defbody">
Set the current output stream to become <var>Stream</var>. See also
<a id="idx:withoutputto2:910"></a><a class="pred" href="IO.html#with_output_to/2">with_output_to/2</a>.
</dd>
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="current_input/1"><strong>current_input</strong>(<var>-Stream</var>)</a></dt>
<dd class="defbody">
Get the current input stream. Useful for getting access to the status
predicates associated with streams.
</dd>
<dt class="pubdef"><span class="pred-tag">[ISO]</span><a id="current_output/1"><strong>current_output</strong>(<var>-Stream</var>)</a></dt>
<dd class="defbody">
Get the current output stream.
</dd>
</dl>
<p><h3 id="sec:write-on-atom"><a id="sec:4.16.5"><span class="sec-nr">4.16.5</span> <span class="sec-title">Write
onto atoms, code-lists, etc.</span></a></h3>
<a id="sec:write-on-atom"></a>
<dl class="latex">
<dt class="pubdef"><a id="with_output_to/2"><strong>with_output_to</strong>(<var>+Output,
:Goal</var>)</a></dt>
<dd class="defbody">
Run <var>Goal</var> as <a id="idx:once1:911"></a><a class="pred" href="metacall.html#once/1">once/1</a>,
while characters written to the current output are sent to <var>Output</var>.
The predicate is SWI-Prolog-specific, inspired by various posts to the
mailinglist. It provides a flexible replacement for predicates such as
sformat/3 , <a id="idx:swritef3:912"></a><a class="pred" href="format.html#swritef/3">swritef/3</a>,
<a id="idx:termtoatom2:913"></a><a class="pred" href="manipatom.html#term_to_atom/2">term_to_atom/2</a>, <a id="idx:atomnumber2:914"></a><a class="pred" href="manipatom.html#atom_number/2">atom_number/2</a>
converting numbers to atoms, etc. The predicate <a id="idx:format3:915"></a><a class="pred" href="format.html#format/3">format/3</a>
accepts the same terms as output argument.
<p>Applications should generally avoid creating atoms by breaking and
concatenating other atoms, as the creation of large numbers of
intermediate atoms generally leads to poor performance, even more so in
multithreaded applications. This predicate supports creating difference
lists from character data efficiently. The example below defines the DCG
rule <a id="idx:term1:916"></a><span class="pred-ext">term/3</span> to
insert a term in the output:
<pre class="code">
term(Term, In, Tail) :-
with_output_to(codes(In, Tail), write(Term)).
?- phrase(term(hello), X).
X = [104, 101, 108, 108, 111]
</pre>
<dl class="latex">
<dt><b>A Stream handle or alias</b></dt>
<dd class="defbody">
Temporarily switch current output to the given stream. Redirection using
<a id="idx:withoutputto2:917"></a><a class="pred" href="IO.html#with_output_to/2">with_output_to/2</a>
guarantees the original output is restored, also if
<var>Goal</var> fails or raises an exception. See also <a id="idx:callcleanup2:918"></a><a class="pred" href="metacall.html#call_cleanup/2">call_cleanup/2</a>.</dd>
<dt><strong>atom</strong>(<var>-Atom</var>)</dt>
<dd class="defbody">
Create an atom from the emitted characters. Please note the remark
above.</dd>
<dt><strong>string</strong>(<var>-String</var>)</dt>
<dd class="defbody">
Create a string object as defined in <a class="sec" href="strings.html">section
4.24</a>.</dd>
<dt><strong>codes</strong>(<var>-Codes</var>)</dt>
<dd class="defbody">
Create a list of character codes from the emitted characters, similar to
<a id="idx:atomcodes2:919"></a><a class="pred" href="manipatom.html#atom_codes/2">atom_codes/2</a>.</dd>
<dt><strong>codes</strong>(<var>-Codes, -Tail</var>)</dt>
<dd class="defbody">
Create a list of character codes as a difference list.</dd>
<dt><strong>chars</strong>(<var>-Chars</var>)</dt>
<dd class="defbody">
Create a list of one-character atoms from the emitted characters,
similar to <a id="idx:atomchars2:920"></a><a class="pred" href="manipatom.html#atom_chars/2">atom_chars/2</a>.</dd>
<dt><strong>chars</strong>(<var>-Chars, -Tail</var>)</dt>
<dd class="defbody">
Create a list of one-character atoms as a difference list.
</dd>
</dl>
</dd>
</dl>
<p></body></html>
|