MTA:Eir/FileSystem/file/eof

From Multi Theft Auto: Wiki
Revision as of 14:55, 31 January 2014 by The GTA (talk | contribs) (Created page with "__NOTOC__ This function returns whether the runtime can still read data from the file/stream. This is an implementation defined state that should return false if no data can be i...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This function returns whether the runtime can still read data from the file/stream. This is an implementation defined state that should return false if no data can be immediately received using read operations. Asynchronous file/stream objects could use this function to return true when new data is available to receive from the socket. If wanting to read an entire file/stream object, this function is more reliable than the file.size approach.

Syntax

boolean file.eof ()

Returns

Returns true if data is available to be read from the file/stream object. Return false if data is not immediately available to receive from it.

Example

Click to collapse [-]
Client

This snippet is guaranteed to read an entire file/stream object.

local fileGetContentSafe( theFile )
    -- Allocate a string buffer where all information is saved in.
    local buffer = "";

    -- Read from the stream until it cannot anymore.
    while not ( theFile.eof() ) do
        buffer = buffer .. theFile.read( 1024 );
    end

    -- At this point, some implementations could wait for data.
    -- Implementations are advised to use blocking I/O, implement additional Lua functions or throw exceptions.

    -- Clean up the file/stream handle and return the result buffer.
    theFile.destroy();
    return buffer;
end

FileSystem File Functions