2.4.2.3. - Reading From Files
The first function below is for reading strings from a file one line at a time. The second and third functions cause spec to switch its source of command input from the keyboard to the specified files.
getline(file [, arg ])
-
This function reads successive lines from the ASCII file
file
each time it is called and returns the string so obtained, including the trailing newline. Ifarg
is the string"open"
, the function returns zero if the file can opened for reading. Otherwise -1 is returned. Ifarg
is"close"
, the file is closed and zero is returned. Ifarg
is zero, the first line of the file is returned. If only the first argument is present, the next line of the file is read and returned. At the end of the file, a -1 is returned.
The previous file, if any, is closed and the new file is opened automatically when the filename argument changes.
To distinguish between an end of file, an error or a literal -1,getline()
assigns a value to a built-in variable namedGETLINE_EOF
(as of spec release 6.03.05). The value ofGETLINE_EOF
will be 1 if there was an end-of-file condition on the read of the file, -1 if there was an error reading the file or if the file couldn't be opened and zero if the read was successful. dofile(file [, line_num|search_pattern ])
-
Queues the file
file
for reading commands.file
must be a string constant or expression. Returns nonzero if the file doesn't exist or permit read access. An optional second argument can specify a line number or a text pattern that will be used to locate the point in the file to begin reading. If the argument is an integer, the number specifies at which line to start reading the file. (Currently, only positive integers are allowed.) If the argument is anything else, it is considered a search string, and text is read from the file starting at the first line containing that search string. The metacharacters*
, which matches any string, and?
, which matches any single character, are allowed in the search string. Initial and trailing white space is ignored in the file. qdofile(file)
- As above, but does not show the contents of the file on the screen as the file is read.
Normally, most errors that occur while reading from a command file cause spec to reset to the level of the main interactive prompt with any open command files then closed. The
spec_par()
"keep_going"
option
is available to override that behavior.
See
spec_par()
2.4.1.2
When a command file is opened within a statement block, the source of input isn't switched to the command file until all the commands in the statement block are executed. Thus it isn't possible to execute commands from a command file within a statement block. Note, though, the
getline()
(2.4.2.3
function is available to scan strings
from files.
When multiple command files are queued on a single command line, the input source can only change after the current line is exhausted, as the following example demonstrates:
1.FOURC> print "hi";dofile("file1");dofile("file2");print "bye
hi bye FOURC.2> Reading "file2". print "This is text from file2" This is text from file2 FOURC.1> Reading "file1". print "This is text from file1" This is text from file1 2.FOURC>
Here file1 contains the single line
print "This is text from file1"
,
and
file2
contains
print "This is text from file2"
.