EOF()

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

Determines whether end of file has been reached when reading from a sequential or binary file. If so, -1 is returned, otherwise 0 is returned.
See Sequential Files and Binary Files.

Usage

Use one of these expressions in a loop or conditional statement to test for eof.

  1. eof(#handle) = 0 ' Not at end of file
  2. eof(#handle) = -1 ' End of file reached
  3. eof(#handle) <> -1 ' Not at end of file
  4. not(eof(#handle)) ' Not at end of file

Note: Testing for eof with a value other than 0 or -1 will cause an error, or possibly incorrect results.

Hints

Using the EOF() function is a classical way of reading all the information from a data file into an array when the total number of pieces of information may not be known. You might do this if you wanted to preserve your data before writing new data to the file in OUTPUT mode, which would destroy the original contents.
In today's computer systems, when reading or writing text files, there is no actual EOF character. The file system knows how long the file is and reads or writes until that length is met. Therefore, you cannot check for a particular character representing the end of the file - unless you add one yourself.

Example

'create a sample file
open "test.txt" for output as #1
'write some data with line delimiters
print #1, "line one "
print #1, "line two "

'write some data without line delimiters
print #1, "item three ";
print #1, "item four ";

'more data with line delimiters added
print #1, "item five"
print #1, "done"
close #1

'INPUT till file ends
open "test.txt" for input as #1
while not(eof(#1))
    INPUT #1, txt$
    print "INPUT item is: ";txt$
wend
close #1
' Try other forms of the test in the While loop to see what happens.

Useful Procedures

' Place a useful function or sub using this keyword here