MTA:Eir/FileSystem/file/seek: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ This function changes the location of the current stream pointer. Like that you can skip file sections, set the stream pointer to a specific location or head to the end...")
 
No edit summary
 
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
int file.seek ( int offset, string mode )
int file:seek ( int offset, string mode )
</syntaxhighlight>
</syntaxhighlight>


Line 20: Line 20:
local function alternativeFileReset( theFile )
local function alternativeFileReset( theFile )
     -- Get the current stream position.
     -- Get the current stream position.
     local streamOffset = theFile.tell();
     local streamOffset = theFile:tell();


     -- Check whether this operation is supported.
     -- Check whether this operation is supported.
Line 28: Line 28:


     -- Reset the file to its beginning.
     -- Reset the file to its beginning.
     theFile.seek( -streamOffset, "cur" );
     theFile:seek( -streamOffset, "cur" );
     return true;
     return true;
end
end

Latest revision as of 23:48, 16 January 2022

This function changes the location of the current stream pointer. Like that you can skip file sections, set the stream pointer to a specific location or head to the end of the file/stream object. Not all stream classes have to support this operation.

Syntax

int file:seek ( int offset, string mode )

Arguments

  • offset: the amount of bytes that should be used for the seeking operation
  • mode: a specifier that describes where the offset should start at; can be set (beginning of file), cur (current stream offset) or end (end of file/stream)

Returns

Returns 0 if the operation was successful, otherwise a non-zero value describing an error. If the operation is not supported by the underlying stream class, it returns false.

Example

Click to collapse [-]
Client

This snippet seeks back to the beginning of the file by going back the current stream pointer byte offset.

local function alternativeFileReset( theFile )
    -- Get the current stream position.
    local streamOffset = theFile:tell();

    -- Check whether this operation is supported.
    if not ( streamOffset ) then
        return false, "not supported";
    end

    -- Reset the file to its beginning.
    theFile:seek( -streamOffset, "cur" );
    return true;
end

FileSystem File Functions