FileRead: Difference between revisions
Jump to navigation
Jump to search
(New page: string fileRead ( file, number count ) Reads the given number of bytes from the given file and returns it as a string. Note that the returned string might not have as many bytes as you sp...) |
Tag: Undo |
||
(12 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | |||
{{Server client function}} | |||
Reads the | Reads the specified number of bytes from the given file starting at its current read/write position, and returns them as a string. | ||
==Syntax== | |||
<syntaxhighlight lang="lua"> | |||
string fileRead ( file theFile, int count ) | |||
</syntaxhighlight> | |||
{{OOP||[[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. | |||
<syntaxhighlight lang="lua"> | |||
function readFile(path) | |||
local file = fileOpen(path) -- attempt to open the file | |||
if not file then | |||
return false -- stop function on failure | |||
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) | |||
</syntaxhighlight> | |||
[[fileOpen]] sets the read/write position to the beginning of the file. | |||
[[fileGetSize]] gets the total size in bytes of given file. | |||
==See Also== | |||
{{File functions}} | |||
[[pt-br:fileRead]] |
Latest revision as of 19:21, 14 August 2024
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 failure 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.