open(@source_sink, @io_mode, -stream, @open_options) [ISO]

This predicate opens a source/sink which is a stream with specified open-options. The open-options is a list of stream options with no variables as an element.

The first parameter is the location of the source/sink, if the location contains backslash characters then be sure to replace each single backslash character with two backslash characters, for instance: if the file to open is 'c:\temp\test.txt' then the value of the first parameter must be 'c:\\temp\\test.txt' (see also escape sequences).

The second parameter must be one of the following predefined atoms:

If a stream is successfully opened then the third parameter '-stream' is assigned a non atomic ground term.

The open-options list supports the following stream-options:

see also: at_end_of_stream/0 at_end_of_stream/1 close/1 close/2 close_user_streams/0 current_input/1 current_output/1 flush_output/0 flush_output/1 open/3 prolog I/O set_input/1 set_output/1 set_stream_position/2 set_stream_property/2 stream_property/2

Example
open('c:\\temp\\new3.txt', write, Stream, [alias(str)]). a file 'c:\temp\new3.txt' is created (or overwritten) and the alias 'str' is associated with the text output stream
open('c:\\temp\\new3.txt', read, Stream, [type(binary), reposition(true), alias(newfile), eof_action(eof_code)]). if there is a file at the location 'c:\temp\new3.txt' then it was successfully opened and Stream was assigned the value '$stream(3)', if there are no other streams except the standard streams, the stream is a binary stream which can be repositioned and it has the alias 'newfile' and if input is read past-end-of-stream then the end-of-file code is returned

 

Exceptions  
source_sink is a variable an instantiation_error exception is thrown
source_sink is neither a variable nor a source/sink term a domain_error(source_sink, SourceSink) exception is thrown, variable SourceSink is substituted by the incorrect source/sink term
the source/sink specified by SourceSink does not exist an existence_error(source_sink, SourceSink) exception is thrown
the source/sink specified by SourceSink cannot be opened a permission_error(open, source_sink, SourceSink) exception is thrown
io_mode is a variable an instantiation_error exception is thrown
io_mode is neither a variable nor an atom a type_error(atom, IOMode) exception is thrown
io_mode is an atom but not an I/O mode a domain_error(io_mode, IOMode) exception is thrown
Stream is not a variable a type_error(variable, Stream) exception is thrown
Options is a variable an instantiation_error exception is thrown
Options is a list with an element E which is a variable an instantiation_error exception is thrown
Options is neither a variable nor a list a type_error(list, Options) exception is thrown
an element E of the options list is neither a variable nor a valid stream-option a domain_error(stream_option, E) exception is thrown
an element E of the options list is alias(A) and A is already associated with an open stream a permission_error(open, source_sink, alias(A)) exception is thrown
an element E of the options list is reposition(true) and it is not possible to reposition this stream a permission_error(open, source_sink, reposition(true)) exception is thrown

 

 

info@trinc-prolog.com