Microsoft KB Archive/41043

= No Warning If GET or SEEK Past End of Random File; Use EOF(n) =

Article ID: 41043

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b

-



This article was previously published under Q41043



SUMMARY
When reading a random access file with the GET statement, if you GET or SEEK beyond the number of existing records, then no &quot;END OF FILE&quot; error occurs. The records that GET reads after the end of a random file are simply blank or set to zero. To avoid this behavior, you must do either of the following:


 * 1) Test the value (true or false) of the EOF(n) function after every GET or SEEK statement to determine if you have reached the end of the random access file. OR


 * 1) Calculate the number of records in the random access file by dividing the number of bytes returned from the LOF(n) function by the length (in bytes) of each record. You can then design your program to GET or SEEK up to, but not greater than, the number of records in the file.



MORE INFORMATION
When working with files OPENed in RANDOM mode, you can use the following steps together to avoid running past the end of the file:


 * 1) Check the value of the EOF(n) function after every SEEK or GET for file number n. If EOF(n) returns -1 (a logical value of true) for a random access file, then the last executed GET statement was unable to read an entire record (which happens when reading past the end of the file).
 * 2) The LOF(n) function returns the length of file number n.
 * 3) The LEN= clause of the OPEN statement specifies the record length for input from a random file.
 * 4) The SEEK and GET statements set the next location to read in the file.
 * 5) The SEEK(n) and LOC(n) functions return your current location in file number n.
 * 6) The GET statement accepts input into a FIELDed buffer or directly into a variable specified as the third argument of the GET.

The following is a code example:

(This example demonstrates how to check the value of the EOF function after every GET.) ' Create a file with RANDOM access as follows: CLS OPEN &quot;junk2&quot; FOR RANDOM AS #1 LEN = 10 FIELD #1, 10 AS x$ LSET x$ = &quot;1234567890&quot; FOR i = 1 TO 5 PUT #1, i NEXT CLOSE

' Input from the existing RANDOM file as follows: OPEN &quot;junk2&quot; FOR RANDOM AS #1 LEN = 10 FIELD #1, 10 AS x$ i=0 DO i=i+1 GET #1, i  IF EOF(1) THEN EXIT DO  ' Exit GET loop when end of file. PRINT i, x$ LOOP CLOSE

Additional query words: QuickBas BasicCom

Keywords: KB41043

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.