EOF(n) Returns True on Random File Until GET Is Executed (63002)



The information in this article applies to:

  • Microsoft BASIC Compiler
  • Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0

This article was previously published under Q63002

SUMMARY

This article provides detailed information on the behavior of the EOF function on a random-access file in QuickBASIC and BASIC programs for the Apple Macintosh.

When the RANDOM file is first opened, the EOF function always returns -1 (true). EOF returns -1 until a GET statement is executed. Once the first GET statement is executed, EOF returns 0 (false) if the GET statement finds a record. EOF returns -1 (true) if there are no more records left in the file or if the last GET statement was unable to read an entire record.

This information applies to Microsoft BASIC Interpreter versions 2.00, 2.10, and 3.00, to Microsoft BASIC Compiler version 1.00, and to Microsoft QuickBASIC version 1.00 for the Apple Macintosh.

MORE INFORMATION

Since the EOF function always returns a true value until a GET statement is executed, you will only want to test for the EOF condition after an attempt has been made to read a random-access record. If you open the file and test for the EOF condition without first reading in a record, the EOF function returns a true value even if there are more records to be read.

The following program duplicates this behavior. The program opens a random file and puts an integer value into the file. It then closes this file, reopens it, and checks for an EOF condition. If the GET statement is commented out, EOF returns a true value even though the end of the file has not yet been reached and there are still valid records and valid data in the file.
   CLS
   OPEN "BOB.DAT" FOR OUTPUT AS #1 'Create a file to be opened later
   CLOSE #1                        'in the program for random-access.

   OPEN "BOB.DAT" AS #1 LEN = 2    'Put an integer into the file.
   FIELD #1, 2 AS var1$
   LSET var1$ = MKI$(5)
   PUT #1, 1
   CLOSE #1

   OPEN "BOB.DAT" AS #1 LEN = 2    'Open the file again to check for EOF.
   FIELD #1, 2 AS var1$

   'GET #1, 1                      'If this line is commented out, EOF
                                   'returns true even though there
                                   'are still records to be read.

   IF EOF(1) THEN PRINT "End of File"
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB63002