Game Development Reference
In-Depth Information
Explicit Functions
This section lists and describes the explicit functions you'll be using in your Lua programming work.
file:close ( )
This function closes the file that is referenced by the file.
file = io.open("myFile.txt", "w")
file:write("Some Data")
file:close()
This function returns an iterator function that returns a new line from the file every time it is called. It
is similar to
io.lines
, with the difference that this function does not close the file when the loop ends.
local file = io.open(filename)
for line in file:lines() do print(line) end
file:close()
file:read ( [format] )
This function reads data from the file according to the given format. By default,
read
reads the entire
next line. The formats that are available for reading are
'*n'
: Reads a number; returns a number instead of a string
'*a'
: Reads the whole file starting from the current position
'*l'
: Reads the next line; returns
nil
when the current position is at the end of
the file (default format)
number
: Reads a string of characters as specified by the number, returning
nil
at
the end of the file.
Following are some examples of using these:
local filename = "data_test.txt"
local file = io.open(filename)