next up previous contents index
Next: 6.1.3 DEC-IO Style File Up: 6.1.2 ISO Streams Previous: 6.1.2 ISO Streams   Contents   Index

6.1.2.1 Other Predicates using ISO Streams

file_reopen(+FileName,+Mode,+Stream,-RetCode)

Takes an existing I/O stream, closes it, then opens it and attaches it to a file. This can be used to redirect I/O from any of the standard streams to a file. For instance,
    | ?- file_reopen('/dev/null', w, 3, Error).
redirects all warnings to the Unix black hole.

On success, RetCode is 0; on error, the return code is negative.

file_clone(+SrcStream,?DestStream,-RetCode)

This is yet another way to redirect I/O. It is a Prolog interface to the C dup and dup2 system calls. If DestStream is a variable, then this call creates a new XSB I/O stream that is a clone of SrcStream. This means that I/O sent to either stream goes to the same place. If DestStream is not a variable, then it must be a number corresponding to a valid I/O stream. In this case, XSB closes DestStream and makes it into a clone of SrcStream.

For instance, suppose that 10 is a I/O Stream that is currently open for writing to file foo.bar. Then

 
| ?- file_clone(10,3,_).
causes all messages sent to XSB standard warnings stream to go to file foo.bar. While this could be also done with file_reopen, there are things that only file_clone can do:
 
| ?- file_clone(1,10,_).
This means that I/O stream 10 now becomes clone of standard output. So, all subsequent I/O will now go to standard output instead of foo.bar.

On success, RetCode is 0; on error, the return code is negative.

file_truncate(+Stream, +Length, -Return)
file_io
The regular file referenced by the StreamStream is chopped to have the size of Length bytes. Upon successful completion Return is set to zero.

Portability Note: Under Windows (including Cygwin) file_truncate/2 is implemented using _chsize(), while on Unix ftruncate() is used. There are minor semantic differences between these two system calls, which are reflected by the behavior of file_truncate/2 on different platforms.

tmpfile_open(-Stream)

Opens a temporary file with a unique filename. The file is deleted when it is closed or when the program terminates.


next up previous contents index
Next: 6.1.3 DEC-IO Style File Up: 6.1.2 ISO Streams Previous: 6.1.2 ISO Streams   Contents   Index
Terrance Swift 2007-10-05