EOF
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgEof
- 最后更新: 2022-03-25
检查是否已到达打开文件的末尾
语法
declare function Eof ( byval filenum as long ) as long用法
result = Eof( filenum )参数
filenum
打开文件的文件号。
返回值
若已到达文件末尾则返回真值(-1),否则返回零(0)。
说明
从以 Input(文件模式) 打开的文件中读取时,了解何时到达文件末尾非常有用,从而避免读取超过文件末尾引发的错误。使用 EOF 来判断这一点。EOF 需要已打开文件的有效文件号。使用 Freefile 获取可用的文件号。
对于以 Output 方式打开的文件,EOF 始终返回 0。
示例
start GeSHi
vb
'' This code finds a free file number to use and attempts to open the file
'' "file.ext" and if successful, binds our file number to the opened file. It
'' reads the file line by line, outputting it to the screen. We loop until eof()
'' returns true, in this case we ignore the loop if file is empty.
Dim As String file_name
Dim As Long file_num
file_name = "file.ext"
file_num = FreeFile( ) '' retrieve an available file number
'' open our file and bind our file number to it, exit on error
If( Open( file_name For Input As #file_num ) ) Then
Print "ERROR: opening file " ; file_name
End -1
End If
Do Until EOF( file_num ) '' loop until we have reached the end of the file
Dim As String text
Line Input #file_num, text '' read a line of text ...
Print text '' ... and output it to the screen
Loop
Close #file_num '' close file via our file number
End 0end GeSHi
与 QB 的差异
- 在 QB 中,当串口没有字符等待读取时会发出 EOF 信号。
- 在 QB 中,对于以 RANDOM 或 BINARY 模式打开的文件,仅在尝试读取超过文件末尾之后,EOF 才返回非零值。在 FreeBASIC 中,读取最后一项后 EOF 即返回真值。
另请参阅
LofLocFreefile
返回 目录