EOF: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
Tag: Manual revert
No edit summary
Line 50: Line 50:
Hello
Hello


Hello World!
Hello�World!
{{OutputEnd}}
{{OutputEnd}}



Revision as of 20:57, 28 January 2023

The EOF function indicates that the end of a file or HTTP response has been reached.


Syntax

endReached%% = EOF([#]fileNumber&)
endReached%% = EOF([#]httpHandle&)


Description

  • fileNumber& or httpHandle& is the number of the file or HTTP connected being read. # is not required.
    • fileNumber& is a file opened using OPEN.
    • httpHandle& is a HTTP connection opened using _OPENCLIENT.
  • Returns 0 until the end of a file. This avoids a file read error.
  • Returns -1 (true) at the end of the file.

Notes

  • In files opened with the INPUT (file mode) the EOF function returns true after any used input function reads a CHR$(26) (Ctrl-Z) from the file, which denotes the "logical" end of a file. This is not necessarily equal to the "physical" end.
    • Although this subtle behavior is not required nowadays, it is still here for the sake of compatibility. If you're interested in the historic cause of it see this Article.
    • To be able to read those files completely use the BINARY (file mode) instead, which is also much faster when used in conjunction with the regular INPUT, LINE INPUT and INPUT$ functions.
  • GET can return invalid data at the end of a file. Read EOF after a GET operation to see if the end of the file has been reached and discard the last read if required.
    • This is not a problem when using GET with HTTP connections with a variable length string, the string will always only contain valid data or be empty.


Examples

Example 1
Showing the difference between INPUT and BINARY file modes when Ctrl-Z is involved.
'Write a simple test file with Ctrl-Z in the middle.
OPEN "test.txt" FOR OUTPUT AS #1
PRINT #1, "Hello"; CHR$(26); "World!"
CLOSE #1

'Now read it back, but uhh, this gives us the "Hello"
'only because of the Ctrl-Z.
OPEN "test.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
    PRINT INPUT$(1, 1);
WEND
CLOSE #1

PRINT: PRINT

'However, it works in the BINARY file mode.
OPEN "test.txt" FOR BINARY AS #1
WHILE NOT EOF(1)
    PRINT INPUT$(1, 1);
WEND
CLOSE #1
Hello

Hello�World!


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link