FileSetPos: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(New page: number fileSetPos ( file, number offset ) Sets the current read/write position in the file. Offset is number of bytes from beginning of file. Returns where the offset was actually set at....)
 
m (Portuguese version indexed)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
number fileSetPos ( file, number offset )
__NOTOC__
{{Server client function}}


Sets the current read/write position in the file. Offset is number of bytes from beginning of file. Returns where the offset was actually set at. Ie if it was above end of file, it will be set at the end of the file.
Sets the current read/write position in the file.
 
==Syntax==
<syntaxhighlight lang="lua">
int fileSetPos ( file theFile, int offset )
</syntaxhighlight>
{{OOP||[[file]]:setPos|pos|fileGetPos}}
 
===Required Arguments===
*'''theFile:''' The file handle of which you want to change the read/write position.
*'''offset:''' The new position. This is the number of bytes from the beginning of the file. If this value is larger than the file size, it is limited to 52,428,800 bytes (50 MB).
 
===Returns===
Returns where the offset was actually set at. I.e. if '''offset''' was past the end of the file, it will be set at the end of the file, and this position will be returned. Returns ''false'' in case of failure (e.g. the specified file handle is invalid).
 
==Example==
This example opens a binary file and prints the value of the byte at position 8 to the console.
<syntaxhighlight lang="lua">
local hFile = fileOpen("test.dat")          -- attempt to open the file
if hFile then                              -- check if it succeeded
    fileSetPos(hFile, 8)                    -- set the read/write position
    local readByte = fileRead(hFile, 1)    -- read one byte from this position
    outputConsole("Byte at position 8 = " .. string.byte(readByte))    -- output it
    fileClose(hFile)                        -- close the file
else
    outputConsole("Unable to open test.dat")
end
</syntaxhighlight>
 
==See Also==
{{File functions}}
[[pt-br:fileSetPos]]

Latest revision as of 20:03, 20 December 2023

Sets the current read/write position in the file.

Syntax

int fileSetPos ( file theFile, int offset )

OOP Syntax Help! I don't understand this!

Method: file:setPos(...)
Variable: .pos
Counterpart: fileGetPos


Required Arguments

  • theFile: The file handle of which you want to change the read/write position.
  • offset: The new position. This is the number of bytes from the beginning of the file. If this value is larger than the file size, it is limited to 52,428,800 bytes (50 MB).

Returns

Returns where the offset was actually set at. I.e. if offset was past the end of the file, it will be set at the end of the file, and this position will be returned. Returns false in case of failure (e.g. the specified file handle is invalid).

Example

This example opens a binary file and prints the value of the byte at position 8 to the console.

local hFile = fileOpen("test.dat")          -- attempt to open the file
if hFile then                               -- check if it succeeded
    fileSetPos(hFile, 8)                    -- set the read/write position
    local readByte = fileRead(hFile, 1)     -- read one byte from this position
    outputConsole("Byte at position 8 = " .. string.byte(readByte))     -- output it
    fileClose(hFile)                        -- close the file
else
    outputConsole("Unable to open test.dat")
end

See Also