www.riscos.com Technical Support: |
|
This chapter describes how you can create data files to read information from files, and how you can create command files to build up a sequence of commands to BASIC.
Programs can create and read information from files, called data files. For example, if you write a program that creates a list of names and telephone numbers, you may wish to save the names and telephone numbers as a data file.
The data file is specified in a program by one of the OPENxx keywords. For example you can create a data file using the keyword OPENOUT.
For example, typing
A = OPENOUT "books"
creates a data file named books and opens it so that it is ready to receive data. The value stored in the variable A is called a channel number and allows the computer to distinguish this data file from other open data files. All future communication with the file books is made via the file channel number in A rather than via the name of the file.
Writing information to a data file is done using PRINT#. For example:
10 A = OPENOUT "books" 20 FOR I = 1 TO 5 30 READ book$ 40 PRINT#A, book$ 50 NEXTI 60 CLOSE#A 70 END 80 DATA "Black Beauty" 90 DATA "Lord of the Rings" 100 DATA "The Wind in the Willows" 110 DATA "The House at Pooh Corner" 120 DATA "The BBC BASIC Reference Manual"
Use CLOSE# to close a data file. This ensures that any data belonging to the file which is still in a memory buffer is stored on the disc. The buffer can then be re-used for another file. After a CLOSE, the file handle is no longer valid.
You can read data from a file using OPENIN and INPUT#. OPENIN opens an existing data file so that information may be read from it. INPUT# then reads the individual items of data. For example:
10 channel = OPENIN "books" 20 REPEAT 30 INPUT#channel, title$ 40 UNTIL EOF#channel 50 CLOSE#channel 60 END
EOF# is a function which returns TRUE when the end of a file is reached.
Other useful keywords for reading or writing data are:
The following writes all the upper-case letters to a file using BPUT# as part of the program:
10 channel = OPENOUT "characters" 20 FOR N% = ASC("A") TO ASC("Z") 30 BPUT#channel,N% 40 NEXT N% 50 CLOSE#channel
BGET# is used as part of a program that allows each character to be read into a string as follows:
10 channel = OPENIN "characters" 20 string$ = "" 30 REPEAT 40 string$ += CHR$(BGET#channel) 50 UNTIL EOF#channel 60 CLOSE#channel
The BPUT# statement and GET$# function can also be used to write text to a file, and read text from a file. These write and read the text in a form compatible with other programs, such as text editors like Edit, unlike PRINT# and INPUT# which write and read strings in BASIC string format.
When you PRINT# an expression to a file, it is written as an encoded sequence of bytes. For example, an integer is stored on the file as the byte &40 followed by the binary representation of the number. A string is written as &00 followed by the length of the string, followed by the string itself in reverse order.
To write information as pure text, you can use:
BPUT#channel,string[;]
The characters of the string, which may be any string expression, are written to the file. If there is no semi-colon at the end of the statement, then a newline character (ASCII 10) is written after the last character of the string. If the semi-colon is present, no newline is appended to the string.
To read an ASCII string from a file, you can use:
str$=GET$#channelThis function reads characters from the file until a newline (ASCII 10), carriage return (ASCII 13 and CHR$13), or null (ASCII 0 and CHR$0) character is read. This terminates the string, but is not returned as part of it. Thus any newlines will look like new strings when you read the file. The end of file also terminates the string.
A command file is a file whose contents are a sequence of commands to be executed as if they had been typed at the keyboard. You can use a variety of methods to create a command file. Using Edit is probably the easiest, especially if that application is already loaded and can be activated from the desktop. See the RISC OS Applications Guide for details on using Edit.
Another way of creating a command file is to use the *BUILD command. If you type
*BUILD keyfile
everything subsequently typed from the keyboard is sent directly to the file called keyfile. If there is a file named keyfile already, it is deleted when the *BUILD command is given.
Press Return at the end of each line. When you finish entering the commands, press Esc to end keyboard input to keyfile.
There are two main ways of executing a command file. If the file contains a sequence of commands to a language, such as BASIC, then you should *EXEC it. For example, suppose you create a file called install which contains the following lines:
INSTALL "basiclib.shell" INSTALL "basiclib.hyperlib" INSTALL "basiclib.debugger" INSTALL "basiclib.FPasm"
The lines in the file are designed to save the programmer from having to type in a list of INSTALL commands whenever BASIC is started. To execute these commands, enter BASIC then type the command
*EXEC install
This causes the contents of install to be taken as input, exactly as if it had been typed in (but much quicker!). You can make the command even shorter by setting the file type of install to COMMAND using the command
*SETTYPE install COMMAND
This converts the file into a runnable file. Once you have done this, you can *EXEC the file just by giving its name as a command, for example:
*install
The other way in which a command file can be executed is to *OBEY it.
Note: If you do this, each line in the file is executed as a * command, i.e. it is passed to the operating system command line interpreter only - not to BASIC. In this case you do not see the lines that are being executed on the screen, and *OBEY allows parameter substitution.
See the section Command scripts in the chapter Notes for command line users in the RISC OS 3 User Guide for more details on *OBEY.