User Tools


Function OPEN

Syntax

  • open filespec &key direction element-type if-exists if-does-not-exist external-formatstream

Arguments and Values

Description

open creates, opens, and returns a file stream that is connected to the file specified by filespec. Filespec is the name of the file to be opened.

If the filespec designator is a stream, that stream is not closed first or otherwise affected.

The keyword arguments to open specify the characteristics of the file stream that is returned, and how to handle errors.

If direction is :input or :probe, or if if-exists is not :new-version and the version component of the filespec is :newest, then the file opened is that file already existing in the file system that has a version greater than that of any other file in the file system whose other pathname components are the same as those of filespec.

An implementation is required to recognize all of the open keyword options and to do something reasonable in the context of the host operating system. For example, if a file system does not support distinct file versions and does not distinguish the notions of deletion and expunging, :new-version might be treated the same as :rename or :supersede, and :rename-and-delete might be treated the same as :supersede.

:direction

These are the possible values for direction, and how they affect the nature of the stream that is created:

:element-type

The element-type specifies the unit of transaction for the file stream.

If it is :default, the unit is determined by file system, possibly based on the file.

:if-exists

if-exists specifies the action to be taken if direction is :output or :io and a file of the name filespec already exists. If direction is :input, not supplied, or :probe, if-exists is ignored. These are the results of open as modified by if-exists:

  • :error: An error of type file-error is signaled.
  • :new-version: A new file is created with a larger version number.
  • :rename: The existing file is renamed to some other name and then a new file is created.
  • :rename-and-delete: The existing file is renamed to some other name, then it is deleted but not expunged, and then a new file is created.
  • :overwrite: Output operations on the stream destructively modify the existing file. If direction is :io the file is opened in a bidirectional mode that allows both reading and writing. The file pointer is initially positioned at the beginning of the file; however, the file is not truncated back to length zero when it is opened.
  • :append: Output operations on the stream destructively modify the existing file. The file pointer is initially positioned at the end of the file. If direction is :io, the file is opened in a bidirectional mode that allows both reading and writing.
  • :supersede: The existing file is superseded; that is, a new file with the same name as the old one is created. If possible, the implementation should not destroy the old file until the new stream is closed.
  • nil: No file or stream is created; instead, nil is returned to indicate failure.

:if-does-not-exist

if-does-not-exist specifies the action to be taken if a file of name filespec does not already exist. These are the results of open as modified by if-does-not-exist:

  • :error: An error of type file-error is signaled.
  • :create: An empty file is created. Processing continues as if the file had already existed but no processing as directed by if-exists is performed.
  • nil: No file or stream is created; instead, nil is returned to indicate failure.

:external-format

This option selects an external file format for the file:

The only standardized value for this option is :default, although implementations are permitted to define additional external file formats and implementation-dependent values returned by stream-external-format can also be used by conforming programs.

The external-format is meaningful for any kind of file stream whose element type is a subtype of character. This option is ignored for streams for which it is not meaningful; however, implementations may define other element types for which it is meaningful.

The consequences are unspecified if a character is written that cannot be represented by the given external file format.


When a file is opened, a file stream is constructed to serve as the file system's ambassador to the Lisp environment; operations on the file stream are reflected by operations on the file in the file system.

A file can be deleted, renamed, or destructively modified by open.

For information about opening relative pathnames, see section {\secref\MergingPathnames}.

Examples

;;; TODO these examples are more or less useless

(open filespec :direction :probe)

#<Closed Probe File Stream...>

(defparameter *q* (merge-pathnames (user-homedir-pathname) "test"))

*Q*

*q*

#<PATHNAME :HOST NIL :DEVICE device-name :DIRECTORY directory-name :NAME "test" :TYPE NIL :VERSION :NEWEST>

(open filespec :if-does-not-exist :create)

#<Input File Stream...>

(defparameter *s* (open filespec :direction :probe))

*S*

*s*

#<Closed Probe File Stream...>

(truename *s*) #<PATHNAME :HOST NIL :DEVICE device-name :DIRECTORY directory-name :NAME filespec :TYPE extension :VERSION 1> (open s :direction :output :if-exists nil)

NIL

Affected By

The nature and state of the host computer's file system.

Exceptional Situations

If if-exists is :error, (subject to the constraints on the meaning of if-exists listed above), an error of type file-error is signaled.

If if-does-not-exist is :error (subject to the constraints on the meaning of if-does-not-exist listed above), an error of type file-error is signaled.

If it is impossible for an implementation to handle some option in a manner close to what is specified here, an error of type error might be signaled.

An error of type file-error is signaled if (wild-pathname-p //filespec//) returns true.

An error of type error is signaled if the external-format is not understood by the implementation.

The various file systems in existence today have widely differing capabilities, and some aspects of the file system are beyond the scope of this specification to define. A given implementation might not be able to support all of these options in exactly the manner stated. An implementation is required to recognize all of these option keywords and to try to do something "reasonable" in the context of the host file system. Where necessary to accomodate the file system, an implementation deviate slightly from the semantics specified here without being disqualified for consideration as a conforming implementation. If it is utterly impossible for an implementation to handle some option in a manner similar to what is specified here, it may simply signal an error.

With regard to the :element-type option, if a type is requested that is not supported by the file system, a substitution of types such as that which goes on in upgrading is permissible. As a minimum requirement, it should be the case that opening an output stream to a file in a given element type and later opening an input stream to the same file in the same element type should work compatibly.

See Also

Notes

open does not automatically close the file when an abnormal exit occurs.

When element-type is a subtype of character, read-char and/or write-char can be used on the resulting file stream.

When element-type is a subtype of integer, read-byte and/or write-byte can be used on the resulting file stream.

When element-type is :default, the type can be determined by using stream-element-type.

\issue{CHARACTER-PROPOSAL:2-5-2} \issue{STREAM-ACCESS:ADD-TYPES-ACCESSORS} \issue{CHARACTER-PROPOSAL:2-5-2} \issue{PATHNAME-WILD:NEW-FUNCTIONS} \issue{CHARACTER-PROPOSAL:2-5-2} \issue{PATHNAME-LOGICAL:ADD} \issue{FILE-OPEN-ERROR:SIGNAL-FILE-ERROR} \issue{PATHNAME-HOST-PARSING:RECOGNIZE-LOGICAL-HOST-NAMES}