Next Previous Up Top Contents Index

5.11 The STREAMS module

with-open-file

Statement macro

Summary

Runs a body of code within the context of a file stream.

Macro call

with-open-file (stream-var = filename, #rest keys) 
body end => values

Arguments

stream-var
An Dylan variable-namebnf.

filename
An instance of <string>.

keys
Instances of <object>.

body
A Dylan bodybnf.

Values

values
Instances of <object>.

Library

io

Module

streams

Description

Provides a safe mechanism for working with file streams. The macro creates a file stream and binds it to stream-var, evaluates a body of code within the context of this binding, and then closes the stream. The macro calls close upon exiting body.

The values of the last expression in body are returned.

Any keys are passed to the make method on <file-stream>.

Example

The following expression yields the contents of file foo.text as a <byte-vector>:

with-open-file (fs = "foo.text", element-type: <byte>)
  read-to-end(fs)
end;

It is roughly equivalent to:

begin
  let hidden-fs = #f;     // In case the user bashes fs variable
  block ()
    hidden-fs := make(<file-stream>, 
                    locator: "foo.text", element-type: <byte>);
    let fs = hidden-fs;
    read-to-end(fs);
  cleanup
    if (hidden-fs) close(hidden-fs) end;
  end block;
end;

See also

close, page 65

<file-stream>, page 70

make file-stream-class, page 75


System and I/O Reference - 31 MAR 2000

Next Previous Up Top Contents Index