MTA:Eir/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 i...")
 
No edit summary
 
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
boolean file.eof ()
boolean file:eof ()
</syntaxhighlight>
</syntaxhighlight>


Line 19: Line 19:


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


Line 27: Line 27:


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

Latest revision as of 23:49, 16 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