Reading Files

io.read() is used to read contents from a file. This function often uses the Colon Operator shorthand to pass the file as the 1st argument. For example:

local Content = File:read('*a') -- read entire file

You can specify a format to read the contents. For each , the function returns a string (or a number) with the characters read, or nil if it cannot read data with the specified format:

“*l”

reads the next line (skipping the end of line), returning nil on end of file (default)

number

reads a string with up to this number of characters, returning nil on end of file.
If number is zero, it reads nothing and returns an empty string or nil on end of file.

“*a”

reads the entire file starting at the current position. On end of file, it returns the empty string.

“*n”

reads a number - this is the only format that returns a number instead of a string.

 

Related pages