appendopens the file for writing, positioning the file pointer at the end. Mode
updateopens the file for writing, positioning the file pointer at the beginning of the file without truncating the file. Stream 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.82New code should use the
alias(Alias)option for compatibility with the ISO standard.
SWI-Prolog also allows SrcDest to be a term
In this form, Command is started as a child process and if
write, output written to Stream
is sent to the standard input of Command. Viso versa, if Mode
read, data written by Command to the standard
output may be read from Stream. On Unix systems, Command
is handed to popen() which hands it to the Unix shell. On Windows, Command
is executed directly. See also process_create/3
The following Options are recognised by open/4:
- Gives the stream a name. Below is an example. Be careful with this
option as stream names are global. See also set_stream/2.
?- open(data, read, Fd, [alias(input)]). ..., read(input, Term), ...
- Check for a BOM (Byte Order Marker) or write one. If omitted,
the default is
write. See also stream_property/2 and especially section 184.108.40.206 for a discussion of this feature.
- Defines output buffering. The atom
full(default) defines full buffering,
linebuffering by line, and
falseimplies 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 flush_output/[0,1]. This option is not an ISO option.
true(default), the stream is closed on an abort (see abort/0). If
false, 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
- Specifies how a new file is created when opening in
updatemode. Currently, List is a list of atoms that describe the permissions of the created file.83Added after feedback from Joachim Shimpf and Per Mildner. Defined values are below. Not recognised values are silently ignored, allowing for adding platform specific extensions to this set.
- Allow read access to the file.
- Allow write access to the file.
- Allow execution access to the file.
- Allow read and write access to the file.
- Allow any access provided by the OS.
Note that if List is empty, the created file has no associated access permissions. The create options map to the POSIX mode option of open(), where
readmap to 0444,
writeto 0222 and
executeto 0111. On POSIX systems, the final permission is defined as (mode &
- Define the encoding used for reading and writing text to this stream.
The default encoding for type
textis derived from the Prolog flag encoding. For
binarystreams the default encoding is
octet. For details on encoding issues, see section 2.19.1.
- Defines what happens if the end of the input stream is reached. Action
eof_codemakes get0/1 and friends return -1, and read/1 and friends return the atom
end_of_file. Repetitive reading keeps yielding the same result. Action
eof_code, but repetitive reading will raise an error. With action
reset, Prolog will examine the file again and return more data if the file has grown.
- Set the locale that is used by notably format/2 for output on this stream. See section 4.23.
- Try to obtain a lock on the open file. Default is
none, which does not lock the file. The value
sharedmeans other processes may read the file, but not write it. The value
exclusivemeans no other process may read or write the file.
Locks are acquired through the POSIX function fcntl() using the command
F_SETLKW, which makes a blocked call wait for the lock to be released. Please note that fcntl() locks are advisory 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!
lockoption is a SWI-Prolog extension.
- Using type
text(default), Prolog will write a text file in an operating system compatible way. Using type
binarythe bytes will be read or written without any translation. See also the option
- This option can be combined with the
true), the open call returns immediately with an exception if the file is locked. The exception has the format
permission_error(lock, source_sink, SrcDest).
reposition is not supported in SWI-Prolog.
All streams connected to a file may be repositioned.