Skip to main content

ReadFile

This function was added in DSL 4

Description

Read bytes from file.

function ReadFile(file, bytes) --[[ ... ]] end

Parameters

  • file: userdata - The file handle to read from. This should be the same handle returned by OpenFile.
  • bytes: number - The number of bytes to read from the file. If the file has fewer bytes than requested, it will return only the available bytes.

Return Values

  • data: string - The data read from the file. If the end of the file is reached, it returns an empty string.

Example

local file, bytes = OpenFile('data.txt', 'r')
if file then
-- highligh-next-line
local data = ReadFile(file, 100) -- Read up to 100 bytes from the file
if data ~= '' then
print('Data read from file:', data)
else
print('End of file reached or no data read')
end
CloseFile(file)
else
print('Failed to open file')
end

See Also