FileGetPos: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Needs example)
m (Portuguese version indexed)
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server function}}
{{Server client function}}


Returns the current read/write position in the given file.
Returns the current read/write position in the given file.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua"> int fileGetPos ( file theFile ) </syntaxhighlight>
int fileGetPos ( file theFile )
{{OOP||[[file]]:getPos|pos|fileSetPos}}
</syntaxhighlight>
 
===Required Arguments===
===Required Arguments===
*'''theFile:''' the file handle you wish to get the position of.
*'''theFile:''' the file handle you wish to get the position of.
Line 16: Line 14:


==Example==
==Example==
<syntaxhighlight lang="lua">
This example opens the file test.txt and outputs its contents and current read position to the console.
--TODO
<syntaxhighlight lang="lua">local hFile = fileOpen("test.txt", true)      -- attempt to open the file (read only)
if hFile then                                  -- check if it was successfully opened
    local buffer
    while not fileIsEOF(hFile) do              -- as long as we're not at the end of the file...
        buffer = fileRead(hFile, 500)          -- ... read the next 500 bytes...
        outputConsole(buffer.."Current Position: "..fileGetPos(hFile))                  -- ... and output them to the console and outputs the current read position
    end
    fileClose(hFile)                          -- close the file once we're done with it
else
    outputConsole("Unable to open test.txt")
end
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{File functions}}
{{File functions}}
[[Category:Needs Example]]
[[pt-br:fileGetPos]]

Latest revision as of 18:51, 20 December 2023

Returns the current read/write position in the given file.

Syntax

 int fileGetPos ( file theFile ) 

OOP Syntax Help! I don't understand this!

Method: file:getPos(...)
Variable: .pos
Counterpart: fileSetPos


Required Arguments

  • theFile: the file handle you wish to get the position of.

Returns

Returns the file position if successful, or false if an error occured (e.g. an invalid handle was passed).

Example

This example opens the file test.txt and outputs its contents and current read position to the console.

local hFile = fileOpen("test.txt", true)       -- attempt to open the file (read only)
if hFile then                                  -- check if it was successfully opened
    local buffer
    while not fileIsEOF(hFile) do              -- as long as we're not at the end of the file...
        buffer = fileRead(hFile, 500)          -- ... read the next 500 bytes...
        outputConsole(buffer.."Current Position: "..fileGetPos(hFile))                  -- ... and output them to the console and outputs the current read position
    end
    fileClose(hFile)                           -- close the file once we're done with it
else
    outputConsole("Unable to open test.txt")
end

See Also