Q(uick)BASIC Function: EOF

Quick View

EOF

A file I/O function that tests for the end-of-file condition

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • EOF(filenumber%)
Description/Parameter(s)
filenumber% The number of an open file.

EOF returns true (nonzero) if the end of a file has been reached.

Example
CLS OPEN "TEST.DAT" FOR OUTPUT AS #1 FOR i% = 1 TO 10 WRITE #1, i%, 2 * i%, 5 * i% NEXT i% CLOSE #1 OPEN "TEST.DAT" FOR INPUT AS #1 DO LINE INPUT #1, a$ PRINT a$ LOOP UNTIL (EOF(1))
Syntax
  • EOF(filenumber)
Description/Parameter(s)

The EOF function returns -1 (true) if the end of a sequential file has been reached. Use the EOF function to test for end-of-file while inputting data. In this way you may avoid the "Input past end" error message.

When EOF is used with random-access or binary files, it returns "true" if the last executed GET statement was unable to read an entire record. This happens because of an attempt to read beyond the end of the file.

EOF cannot be used with the BASIC devices SCRN:, KYBD:, CONS:, and LPTn:.

When you use EOF with a communications device, the definition of the end-of-file condition is dependent on the mode (ASCII or binary) in which you opened the device. In ASCII mode, EOF is false until you receive CTRL+Z, after which it remains true until you close the device. In binary mode, EOF is true when the input queue is empty (LOC(n)=0). It becomes "false" when the input queue is not empty.

Examples

This example reads single-precision values from the file DATA.IN into an array M using a DO LOOP. The loop uses the EOF function to signal the end of the file. A second DO LOOP displays the contents of M to confirm that it contains the values from DATA.IN.
This example will not run without a DATA.IN data input file.

DIM M(0 TO 2000) OPEN "DATA.IN" FOR INPUT AS 1 C = 0 DO WHILE NOT EOF(1) AND C <= 2000 INPUT #1, M(C) C = C + 1 LOOP CLS ' Clear screen D = 0 DO WHILE D < C PRINT M(D) D = D + 1 LOOP

Tip: Run the example with a simple DATA.IN data file you create with a text editor. For example, create a one-line DATA.IN file with the following sequence of numbers:
10 20 30 40 50 60 70 80 90
Then run the example program to produce the following sample output.

Sample Output:

10 20 30 40 50 60 70 80 90
Syntax
  • EOF(filenumber%)
Description/Parameter(s)
  • The argument filenumber% is the number used in the OPEN statement to open the table.

Usage Notes

  • The EOF function returns true (nonzero) if the end of a non-ISAM file has been reached or if the current position in an ISAM table is at the end of the table. The end of an ISAM table is the position immediately following the last record according to the current index.
  • Use the EOF function with sequential files to test for the end of a file. This helps you avoid an "Input past end" error.
  • When used with random-access or binary files, EOF returns true if the last executed GET statement was unable to read an entire record.
  • When you use EOF with a communications device, the definition of the end-of-file condition depends on the mode (ASCII or binary) in which you opened the device. In ASCII mode, EOF is false until you receive Ctrl+Z, after which it remains true until you close the device. In binary mode, EOF is true when the input queue is empty (LOC(n)=0). It becomes false when the input queue is not empty.

Important

  • EOF cannot be used with the BASIC devices SCRN, KYBD, CONS, LPTn, or PIPE.
Example

This example uses the INPUT # statement to read a series of test scores from a sequential file, then calculates and displays the average score. The program uses the EOF function to determine when the last score has been read from the file.

DEFINT A-Z DIM Total AS LONG CLS 'Create the required input file. OPEN "class.dat" FOR OUTPUT AS #1 PRINT #1, 98, 84, 63, 89, 100 CLOSE #1 'Open the input file just created. OPEN "class.dat" FOR INPUT AS #1 DO WHILE NOT EOF(1) 'Do as long as the end-of-file hasn't been reached. Count = Count + 1 INPUT #1, Score 'Get a score from file #1. Total = Total + Score PRINT Count; Score LOOP PRINT PRINT "Total students:"; Count; " Average score:"; Total / Count END

Sample Output:

1 98 2 84 3 63 4 89 5 100 Total students: 5 Average score: 86.8