|
xxxxxxxxxxxxxxxxxx |
BASIC LESSON 10
|
OPEN "filename" FOR mode AS #filenumber . |
In place of filename you write the name of the file, including the directory address - some examples of names of files are
A:\primes.bas | C:\math\homework\sols.wrk | |
H:\roots.bas | divisors.dat . |
It is best to use the extension 'bas" if you want your file to be easy to find; doing so ensures the file will be listed in the "Open Program" list when you click "File - Open" in Basic. If you do not include the directory address, as in the last example, Basic will assume that the file is located in the same directory as the main Basic program.
In place of mode you write one of the three modes - these are OUTPUT, APPEND, and INPUT. The OUTPUT mode permits Basic to write information to the file. If the file already exists, Basic will overwrite the old file with the new information, destroying all previous contents of the file. If the file does not already exist, Basic will create a file with that name. The APPEND mode allows Basic to write information to the file without destroying the old information - Basic just adds the new information onto the end of the file. Again, if the file does not already exist, Basic will create it. Finally, the INPUT mode permits Basic to read information from the file, such as it would from a DATA statement. If the file does not exist, Basic stops and gives an error message.
In place of filenumber you write a number for the file (an integer between 1 and 255). In subsequent Basic statements you use this number when referring to the file. It is possible to have several files open at once, but each must have a different file number. Here are some examples of OPEN statements:
OPEN "A:\primes.bas" FOR OUTPUT AS #1 |
OPEN "H:\divisors.bas" FOR APPEND AS #6 |
OPEN "C:\math\homework\sols.bas" FOR INPUT AS #143 . |
These three statements allow Basic to overwrite the file "primes", to make additions to the file "divisors", and to read from the file "sols".
CLOSE #5 | CLOSE #5, 7 | CLOSE |
The first of these commands closes the file numbered 5, while the second closes the two files numbered 5 and 7. The third CLOSE command, followed by no number, closes all files. In order to avoid possible memory problems and to increase efficiency, it is prudent to close a file after a program is finished with it.
PRINT #7, "The solution of the equation is"; X |
writes to file # 7 the information listed. Of course a file must be opened before anything can be written to it - so always an OPEN command must precede a PRINT # command. If file # 7 were opened in OUTPUT mode, the information overwrites the file, but if it were opened in APPEND mode the information is added to the end of the file. Note that, in contrast to the ordinary PRINT for printing to the screen, PRINT #7 is followed immediately by a comma before the printing instructions.
Example :
The following sequence opens a file on the A drive, writes to the file the even numbers from 2 to 10 separated by commas, and then closes the file:
OPEN "A:\evens.bas" FOR OUTPUT AS #1 | |
PRINT #1, "The even numbers from 2 to 10 are:" | |
FOR I = 2 TO 10 STEP 2 | |
PRINT #1, I; " , "; | |
NEXT I | |
CLOSE #1 |
You can also combine USING with the PRINT # statement, as in
PRINT #3, USING "The principal is $$#####.##"; P | . |
WRITE #4, "John Smith", 23, "1423 Maple Street", "Topeka, Kansas" | . |
As there is not much use for this command in most mathematical programming, we will not discuss it in detail.
INPUT #4, fullname$, age, address$, city$ | . |
The file must first be opened in INPUT mode before this command can be used. Again, as the INPUT # command is seldom needed in mathematical programming, we will not discuss it in detail.
1) | S = 0 | 2) | S = 0 | |||||
DO UNTIL EOF(3) | DO | |||||||
INPUT # 3, X | INPUT # 3, X | |||||||
S = S + X | S = S + X | |||||||
LOOP | LOOP UNTIL EOF(3) | |||||||
PRINT "THE SUM IS "; S | PRINT "THE SUM IS "; S |
Each of the loops reads numbers from the file assigned number 3, and adds these numbers to calculate the sum of all numbers in the file. The function EOF(3) checks whether the end of the file numbered 3 has been reached - if that is the case the program jumps out of the loop to the next command after the loop. On the left above the check is done at the top of the loop, and on the right it is done at the bottom. The sum is the same in the two cases, and after each loop this sum is printed to the screen.