open

From the SETL Wiki

Jump to: navigation, search
open
Category I/O
Syntax function
Compatibility
CIMS SETL yes
SETL-S no (equivalent: input or output)
SETL2 yes
GNU SETL yes

Contents

Purpose

Open a file

Synopsis

 openOk := open(path, mode); $ CIMS SETL
handle := open(path, mode); -- SETL2, GNU SETL

Description

Opens a file or other resource for input and/or output.

The path argument is usually a file path, but in GNU SETL can take other forms such as an IP address, shell command or existing unix file descriptor.

The mode argument specifies the type of resource and what operations are allowed (e.g. reading, writing, seeking).

In CIMS SETL open returns a boolean value indicating whether the open operation succeeded.
In SETL2 and GNU SETL open returns a handle which can be passed to file-based I/O primitives such as printa. Ideally this handle is treated as opaque, but in SETL2 it is represented as an atom and in GNU SETL as an integer which (usually) corresponds to a UNIX file descriptor.

  • CIMS SETL provides only stream input or output modes (no bidirectional I/O or random access)
  • SETL-S similarly has only stream input and output, but uses a different pair of primitives, input and output, to open the files.
  • SETL2 adds a random access mode.
  • GNU SETL adds new modes for sockets and pipes, and removes the distinction between text and binary modes.

For a complete list, see List of I/O modes.

Examples

  1. h1 := open('xyz.txt', 'text-in');
    reada(h1, x);
    

    Opens the file 'xyz.txt' for input and reads the first value into the variable x

  2. h2 := open('xyz.txt', 'text-out');
    printa(h2, x);
    

    Opens the file 'xyz.txt' for output (overwriting it if it exists) and writes a textual representation of the value of x to it.

  3. (GNU SETL or SETL2)
    h3 := open('abc.dat', 'random');
    puts(h3, 42, 'xxxx');
    

    Opens the file 'abc.dat' for random access (reading and writing) and replace the 42nd through 45th bytes with the ASCII encoding of the string 'xxxx'.

See also

Personal tools