Blitz3D+ Command Reference

Eof ( stream )

Parameters

stream - a file stream (ReadFile, WriteFile, OpenFile) or a TCP stream (OpenTCPStream, AcceptTCPStream)

Description

Returns whether the end of a file or stream has been reached.

The possible return values are:

0 : more data may follow
1 : the end - the file is exhausted, or the TCP connection was closed by the other side
-1 : the stream has failed (for example a TCP read timed out or the connection dropped)

The classic use is a read loop over a file of unknown length: While Not Eof(file) ... Wend. Since both 1 and -1 are "truthy", such a loop stops on errors too.

On a TCP stream, note that Eof=0 does not mean data is waiting right now - only that the connection is still up. Use ReadAvail to see how much has actually arrived before you read, if you do not want the read to block.

See also: ReadAvail, ReadFile, ReadLine, OpenTCPStream.

Example

; Eof Example
; -----------

; Write a short captain's log to read back
file=WriteFile("demo_log.txt")
WriteLine file,"Day 1: Landed on the moon"
WriteLine file,"Day 2: Found a strange crater"
WriteLine file,"Day 3: It was cheese all along"
CloseFile file

; Read the whole file - Eof returns 1 once every line has been read,
; so this loop copes with a file of any length
file=ReadFile("demo_log.txt")
count=0
While Not Eof(file)
    count=count+1
    Print "Line "+count+": "+ReadLine$(file)
Wend
CloseFile file
Print "Eof reached after "+count+" lines"

; Clean up the demo file
DeleteFile "demo_log.txt"
Print "Deleted demo_log.txt"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index