EOF Function |
No
EOF(filenumber)
filenumber
Use: Required
Data: Integer
Any valid file number.
An integer containing –1 (True), or (False).
Returns an integer evaluating to True (–1) when the end of a file has been reached; until the end of the file is reached, EOF returns False (0).
filenumber must be a valid number used in the Open statement to open either a random or sequential file.
If you have opened the file using either random or binary access, a Get statement that can't read a complete record (i.e., an attempt to access a record past the last record in the file) causes EOF to return True.
iFile = FreeFile Open sFilename for Input as #iFile Do While Not EOF(iFile) LineInput #iFile, sDataLine ... Loop Close #iFile
EOF allows you to test whether the end of a file has been reached without generating an error.
Because EOF dates back to the times when VB didn't support an intrinsic Boolean data type, the function uses an integer data type to hold the and –1 False and True values.
Because you always write data to sequential files at the end of the file, the file marker is always at the end of the file, and EOF therefore always returns True when testing files opened with their modes set equal to either output or append.
As Visual Basic is continually enhanced with new functions and new objects, there are more efficient and elegant alternatives to some of the VB language elements that have been with us since before "Visual" was even thought of! The following snippets compare methods of populating an array with data extracted from a comma-delimited text file. The first snippet uses a standard Do...Loop and EOF flag:
Dim sFilename As String Dim sContents As String Dim iFile As Integer Dim sArray() As String iFile = FreeFile sFilename = "testinput.txt" ReDim sArray(0) Open sFilename For Input As #1 Do While Not EOF(1) Input #1, sContents ReDim Preserve sArray(UBound(sArray) + 1) sArray(UBound(sArray)) = sContents Loop Close #1
You can replace this with a single call to the Input function, passing to it the length of the text file, and, if you're using VB6, you can call the Split function to parse the comma-delimited string:
Dim sFilename As String Dim sContents As String Dim iFile As Integer Dim sArray() As String sFilename = "testinput.txt" Open sFilename For Input As #iFile sContents = Input(LOF(iFile), iFile) Close #iFile sArray = Split(sContents, ",")
Again, for VB6 users only, there is the object-oriented way of extracting the data, again passing the resulting string to the Split function to be parsed. For this example to work, you have to create a project reference to the Microsoft Scripting Runtime Library, which gives you access to the File System Object:
Dim sFilename As String Dim sArray() As String Dim ofsFileSys As New Scripting.FileSystemObject Dim ofsTextStream As TextStream sFilename = "testinput.txt" Set ofsTextStream = _ ofsFileSys.OpenTextFile(sFilename) sArray = Split(ofsTextStream.ReadAll, ",") Set ofsTextStream = Nothing
Don't confuse the EOF Function with the EOF property of the RDO, DAO, and ADO recordsets and result sets. The former is a function for testing the position of the file pointer in a file opened using the VBA Open statement; the latter is a property that indicates the state of the record pointer in a database or recordset opened using automation.
The AtEndOfStream property is the TextStream object's equivalent to the EOF function.