read -- search, read, and execute a
file
Introductionread(filename) searches for the file
filename in certain directories, reads and executes
it.
read(n) reads and executes the file
associated with the file descriptor n.
Call(s)read(filename <, Quiet> <, Plain>)
read(n <, Quiet> <, Plain>)
Parametersfilename |
- | the name of a file: a character string |
n |
- | a file descriptor provided by fopen: a positive integer |
OptionsPlain |
- | makes read use its own parser
context |
Quiet |
- | suppresses output during execution of
read |
Returnsthe return value of the last statement of the file.
Related
Functionsfclose, finput, fopen, fprint, fread, ftextinput, input, LIBPATH, loadproc, pathname, print, protocol, READPATH, textinput, write, WRITEPATH
Detailsread(filename) searches for the file in
various directories:
filename is concatenated to each directory given by the
environment variable READPATH.LIBPATH.fread./'' on UNIX or Linux,
``\'' on Windows and ``:'' on the Macintosh)
is inserted as neccessary when concatenating a given path and
filename.read(n) with a file descriptor
n as returned by fopen is equivalent to the call
fread(n).fread for details about reading and
executing the file's content and for a detailed description of the
options Plain and Quiet.
Example
1The following example only works under UNIX and Linux;
on other operating systems one must change the path names accordingly.
First, we use write to
store values in the file ``testfile.mb'' in the
``/tmp'' directory:
>> a := 3: b := 5: write("/tmp/testfile.mb", a, b):
The following command specifies the file by its absolute
path name. After reading the file, the values of a and
b are restored:
>> delete a, b: read("/tmp/testfile.mb"): a, b
3, 5
Alternatively, we define ``/tmp'' as the
search directory and provide a relative path name. Note that the path
separator ``/'' is inserted by read:
>> delete a, b: READPATH := "/tmp": read("testfile.mb"): a, b
3, 5
We may also use fopen to open the file and read its
content. Note that fopen
does not search for the file like read, thus we must enter
an absolute path name or a name relative to the working directory:
>> delete a, b:
n := fopen("/tmp/testfile.mb"): read(n): fclose(n):
a, b
3, 5
>> delete a, b, READPATH, n