Modules/FileSystem/file/eof: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(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 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 MTA:Eir/FileSystem/file/size|f...")
 
No edit summary
 
Line 1: Line 1:
<pageclass class="#3cc882" subcaption="File function"></pageclass>
__NOTOC__
__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 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 [[MTA:Eir/FileSystem/file/size|file.size]] approach.
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 [[MTA:Eir/FileSystem/file/size|file.size]] approach.

Latest revision as of 03:12, 23 January 2022

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