REDUCE

10.5 File System Interface: Open and Close

(open FILENAME:string ACCESSTYPE:id): CHANNEL:io-channel expr
If AccessType is eq to input or output, an attempt is made to access the system-dependent FILENAME for reading or writing. If the attempt is unsuccessful, an error is generated; otherwise a free channel is returned and initialized to the default conditions for ordinary file input or output.

If none of these conditions hold, a file is not available, or there are no free channels, an error is generated.

    ⋆⋆⋆⋆⋆ Unknown access type  
    ⋆⋆⋆⋆⋆ Improperly set-up special IO open call  
    ⋆⋆⋆⋆⋆ File not found  
    ⋆⋆⋆⋆⋆ No free channels

If AccessType is eq to SPECIAL, no file is opened. Instead the channel is initialized as a generalized input and/or output stream. See below.

(filep NAME:string): boolean expr
This function will return t if file NAME can be opened, and nil if not, e.g. if it does not exist.

(close CHANNEL:io-channel): io-channel expr
The closing function associated with CHANNEL is called, with CHANNEL as its argument. If it is illegal to close CHANNEL, if CHANNEL is not open, or if CHANNEL is associated with a file and the file cannot be closed by the operating system, this function generates an error. Otherwise, CHANNEL is marked as free and is returned.

Here is a simple example of input from a particular file with output sent to the current output channel. This function reads forms from the file MYFILE.DAT and prints out all those whose car is eq to its parameter. Using unwind-protect, we are assured that the channel (and the file), will be closed in all cases, including errors.

  (de filter-my-file (x)  
    (let ((chan (open "MYFILE.DAT" 'input))  
          form)  
      (unwind-protect  
        (while (neq (setq form (channelread chan))  
                    $eof$)  
               (if (and (pairp form) (eq (car form) x))  
                 (print form)))  
        (close chan))))