FileRead: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(add oop syntax)
Line 20: Line 20:
This example opens the file test.txt and outputs its contents to the console.
This example opens the file test.txt and outputs its contents to the console.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local hFile = fileOpen("test.txt")             -- attempt to open the file
function readFile(path)
if hFile then                                  -- check if it was successfully opened
    local file = fileOpen(path) -- attempt to open the file
    local buffer
    if not file then
    while not fileIsEOF(hFile) do              -- as long as we're not at the end of the file...
         return false -- stop function on failture
         buffer = fileRead(hFile, 500)          -- ... read the next 500 bytes...
        outputConsole(buffer)                  -- ... and output them to the console
     end
     end
     fileClose(hFile)                           -- close the file once we're done with it
    local count = fileGetSize(file) -- get file's total size
else
    local data = fileRead(file, count) -- read whole file
     outputConsole("Unable to open test.txt")
     fileClose(file) -- close the file once we're done with it
     outputConsole(data) -- output code in console
end
end
addCommandHandler("readFile",function(cmd,fileName) -- add command to test this function
    readFile(fileName) -- execute the function
end)
</syntaxhighlight>
</syntaxhighlight>


[[fileOpen]] sets the read/write position to the beginning of the file. Each call of fileRead will read 500 bytes from the current position, and advance the position over the same amount. If we get near the end of the file and there are less than 500 bytes left to read, all that's left will be read and [[fileIsEOF]] will return ''true''.
[[fileOpen]] sets the read/write position to the beginning of the file.
[[fileGetSize]] gets the total size in bytes of given file


==See Also==
==See Also==
{{File functions}}
{{File functions}}

Revision as of 06:26, 16 September 2018

Reads the specified number of bytes from the given file starting at its current read/write position, and returns them as a string.

Syntax

string fileRead ( file theFile, int count )

OOP Syntax Help! I don't understand this!

Method: file:read(...)


Required Arguments

  • theFile: A handle to the file you wish to read from. Use fileOpen to obtain this handle.
  • count: The number of bytes you wish to read.

Returns

Returns the bytes that were read in a string. Note that this string might not contain as many bytes as you specified if an error occured, i.e. end of file.

Example

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

function readFile(path)
    local file = fileOpen(path) -- attempt to open the file
    if not file then
        return false -- stop function on failture
    end
    local count = fileGetSize(file) -- get file's total size
    local data = fileRead(file, count) -- read whole file
    fileClose(file) -- close the file once we're done with it
    outputConsole(data) -- output code in console
end

addCommandHandler("readFile",function(cmd,fileName) -- add command to test this function
    readFile(fileName) -- execute the function
end)

fileOpen sets the read/write position to the beginning of the file. fileGetSize gets the total size in bytes of given file

See Also