FileHandle.atEnd

From MiniScript Wiki
Revision as of 18:27, 9 January 2021 by JoeStrout (talk | contribs) (Created page with "The <c>atEnd</c> method of the FileHandle class returns true after attempting to read past the end of the file. Note that <c>atEnd</c> is not true after the last successf...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The atEnd method of the FileHandle class returns true after attempting to read past the end of the file.

Note that atEnd is not true after the last successful read, nor is it true at the beginning of an empty file. It only reports the condition that the last attempted read failed because there was no more data. This is explained (in reference to the standard C function feof) here.

For this reason, instead of using atEnd in most cases, you should simply check for the read or readLine method to return null, like so:

reader = file.open("fileToRead.txt", "r")
writer = file.open("fileToWrite.txt", "w")

// Copy the lines from one to the other, while changing to upper case.
while true
	line = reader.readLine
	if line == null then break
	writer.writeLine line.upper
end while