Skip to main content

OpenFile

This function was added in DSL 4

Description

Open a file given a relative path. mode should be 'r' for reading, 'w' for writing, or 'a' for updating.

Files are always opened in binary, and the size of the file is returned in bytes.

Zipped scripts (see IsScriptZipped) can only open files for reading.

function OpenFile(name, mode) --[[ ... ]] end

Parameters

  • name: string - The relative path to the file to open. This can be a path to a file in the script's package or in the script's directory.
  • mode: 'a'|'r'|'w' - The mode to open the file in.

Return Values

  • file: userdata - The file handle, which can be used with other file functions like ReadFile or WriteFile.
  • bytes: number - The size of the file in bytes, or 0 if the file does not exist or could not be opened.

Versions

  • DSL6 - The 'a' mode was added.

Example

local file, bytes = OpenFile('data.txt', 'r')
if file then
print('File opened successfully, size:', bytes)
else
print('Failed to open file')
end

See Also