FileRead: Difference between revisions
Jump to navigation
Jump to search
m (Portuguese version indexed) |
No edit summary |
||
Line 8: | Line 8: | ||
string fileRead ( file theFile, int count ) | string fileRead ( file theFile, int count ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{Added feature/item|1.6.1|1.6.0|22560| | |||
<syntaxhighlight lang="lua"> | |||
string fileRead ( string filePath ) | |||
</syntaxhighlight> | |||
}} | |||
{{OOP||[[file]]:read}} | {{OOP||[[file]]:read}} | ||
Line 13: | Line 19: | ||
*'''theFile:''' A handle to the file you wish to read from. Use [[fileOpen]] to obtain this handle. | *'''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. | *'''count:''' The number of bytes you wish to read. | ||
{{Added feature/item|1.6.1|1.6.0|22560| | |||
*'''filePath:''' Path to the file you want to read | |||
}} | |||
===Returns=== | ===Returns=== | ||
Line 18: | Line 27: | ||
==Example== | ==Example== | ||
<section name="Shared" class="both" show="true"> | |||
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"> | ||
Line 35: | Line 45: | ||
end) | end) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
<section name="Server" class="server" show="true"> | |||
This example reads file from path and prints its content to the console | |||
<syntaxhighlight lang="lua"> | |||
addCommandHandler('readFile', function(player, cmd, file) | |||
if not file then | |||
outputChatBox('You need to provide a path!', player, 255,0,0) | |||
return | |||
end | |||
outputChatBox(fileRead(file), player) | |||
end) | |||
</syntaxhighlight> | |||
</section> | |||
==See Also== | ==See Also== | ||
{{File functions}} | {{File functions}} | ||
[[pt-br:fileRead]] | [[pt-br:fileRead]] |
Revision as of 16:28, 24 June 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
Click to collapse [-]
SharedThis 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)
Click to collapse [-]
ServerThis example reads file from path and prints its content to the console
addCommandHandler('readFile', function(player, cmd, file) if not file then outputChatBox('You need to provide a path!', player, 255,0,0) return end outputChatBox(fileRead(file), player) end)