FileIsEOF: Difference between revisions
Jump to navigation
Jump to search
(New page: bool fileIsEOF ( file ) Returns true if the file read/write position is at the end of the file.) |
m (Portuguese version indexed) |
||
(5 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | |||
{{Server client function}} | |||
Returns true if the file read/ | Checks if the file position is at the end of the file. | ||
{{Note|Due to underlying C API restrictions this function may return false until an attempt to read further than the end of the file is made.}} | |||
==Syntax== | |||
<syntaxhighlight lang="lua"> | |||
bool fileIsEOF ( file theFile ) | |||
</syntaxhighlight> | |||
{{OOP||[[file]]:isEOF|eof}} | |||
===Required Arguments=== | |||
*'''theFile:''' A handle to the file you wish to check. | |||
===Returns=== | |||
Returns ''true'' if the file position of the specified file is at the end of the file, ''false'' otherwise. | |||
==Example== | |||
This example opens the file test.txt and outputs its contents to the console. | |||
<syntaxhighlight lang="lua"> | |||
local hFile = fileOpen("test.txt", true) -- attempt to open the file (read only) | |||
if hFile then -- check if it was successfully opened | |||
local buffer | |||
while not fileIsEOF(hFile) do -- as long as we're not at the end of the file... | |||
buffer = fileRead(hFile, 500) -- ... read the next 500 bytes... | |||
outputConsole(buffer) -- ... and output them to the console | |||
end | |||
fileClose(hFile) -- close the file once we're done with it | |||
else | |||
outputConsole("Unable to open test.txt") | |||
end | |||
</syntaxhighlight> | |||
When you open a file, its file position is set to the beginning of the file. Each call to [[fileRead]] or [[fileWrite]] moves the position ahead by the amount of bytes that were read/written. This way, by using ''fileIsEOF'' you can check if you've passed through the whole file. | |||
==See Also== | |||
{{File functions}} | |||
[[pt-br:fileIsEOF]] |
Latest revision as of 19:04, 20 December 2023
Checks if the file position is at the end of the file.
Syntax
bool fileIsEOF ( file theFile )
OOP Syntax Help! I don't understand this!
- Method: file:isEOF(...)
- Variable: .eof
Required Arguments
- theFile: A handle to the file you wish to check.
Returns
Returns true if the file position of the specified file is at the end of the file, false otherwise.
Example
This example opens the file test.txt and outputs its contents to the console.
local hFile = fileOpen("test.txt", true) -- attempt to open the file (read only) if hFile then -- check if it was successfully opened local buffer while not fileIsEOF(hFile) do -- as long as we're not at the end of the file... buffer = fileRead(hFile, 500) -- ... read the next 500 bytes... outputConsole(buffer) -- ... and output them to the console end fileClose(hFile) -- close the file once we're done with it else outputConsole("Unable to open test.txt") end
When you open a file, its file position is set to the beginning of the file. Each call to fileRead or fileWrite moves the position ahead by the amount of bytes that were read/written. This way, by using fileIsEOF you can check if you've passed through the whole file.