Calculus Computer Lab
Math 190


News

Lab Schedule

QuickBasic Notes

Fortran Notes

Tutor Room

Winplot Manual

Lab Home

xxxxxxxxxxxxxxxxxx

BASIC LESSON 10

Lesson Topics
OPENWRITE #
CLOSEINPUT #
PRINT #EOF (End of File)
View DemosDownload Demos
# 1 # 2 # 3 # 4 # 1 # 2 # 3 # 4
Main Basic Page

There are three types of files for storing data in Basic - namely, sequential files, random-access files, and binary files. We will discuss exclusively sequential files, as these are the simplest and are probably sufficient for most mathematical programming problems. Thus, in the ensuing discussion, the word file refers always to a sequential file.

OPEN

The OPEN command is used to open files - that is, it makes files available so that Basic can read or write to them. The general form of the command is

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

The CLOSE command is used to close one or more files - examples are

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 #

The PRINT # command writes data to a file - the data is written to the file whose number follows "#". The command works like the PRINT command, except that the information is sent to the file instead of printed to the screen. The statement

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 #

The command WRITE # works a lot like PRINT #, sending data to a file. However, WRITE # sends data in a format which can be read later by Basic - it separates items by commas and puts quotation marks around strings, much as you would in typing a DATA statement for Basic to access with a READ statement. This command is useful in programs that process information input by the user, or that generate data that might be read by Basic at some later date. An example of use of this command is

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 #

The INPUT # command reads from a file data that has been written by Basic with the WRITE # command. It works pretty much like the READ command, except that data is read from a file instead of from a DATA statement. Data from the file is read in sequential order, as from DATA statements. An example of use of this command is

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.

EOF (End of File)

Often when reading data from a file a program does not know beforehand the length of the file. If the reading is done from within a loop, there must be a way to stop the loop when all the data is read - otherwise, Basic produces an error message when asked to read more data after the end of the file has been reached. For this purpose Basic employs a function EOF(), that checks whether the last reading of the file used up all the data in the file. Here are examples of the use of this function :

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.