with-open-file

A convenience macro for working on an open file.

Opens a file named name, giving the variable stream to body to perform the stream operations.  It then closes the file when body finishes.  Only the stream variable is visible to bodyBody sees neither name nor parameters.

For example, the below code snippet looks for someone with Dylan in their resume

let line = "";
let dylan-experience = #f;

with-open-file(file = "resume.txt")
while(line & ~ dylan-experience)
dylan-experience := regexp-position(line, "dylan");
line := read-line(file, on-end-of-stream: #f);
end while;
end;

format-out(if(dylan-experience)
"Hire this person now!\n"
else
"Try someone else\n"
end if);

Exported from

Macro call

with-open-file ( (stream) = (locator), (keys) )
(body)
end

Arguments

streamA variable name.  This will be bound to an instance of <file-stream>.
nameAn instance of <file-locator> or <string>.
parametersMake keywords used when creating stream.
bodyA series of semicolon-separated expressions.
The file-system module.
The class of streams over disk files.
A class.
The class of sequences with elements that are characters.