Summary
Reads the file according to the specified formats
Prototype
v1, v2 ... = f:read (format1, format2, ...)
Description
Reads the file f according to the given formats. Each format returns a string, a number, or nil and an error code if it fails (see below). The formats are:
*n - reads a number and returns it
*a - reads the entire file from the current position
*l - (default) - reads the next line, returns nil on EOF
number - returns a string with up that many characters in it, or nil on EOF
f = io.input ("test.txt")
repeat
s = f:read ("*l") -- read one line
if s then -- if not end of file (EOF)
print (s) -- print that line
end
until not s -- until end of file
f:close () -- close that file now
If the file cannot be read the function returns 3 things:
nil
An error message (string)
An error code (number)
See Also ...
Lua functions
f:close - Closes a file
f:flush - Flushes outstanding data to disk
f:lines - Returns an iterator function for reading the file line-by-line
f:seek - Sets and gets the current file position
f:setvbuf - Sets the buffering mode for an output file
f:write - Writes to a file
io.close - Closes a file
io.flush - Flushes outstanding data to disk for the default output file
io.input - Opens filename for input in text mode
io.lines - Returns an iterator function for reading a named file line-by-line
io.open - Opens a file
io.output - Opens a file for output
io.popen - Creates a pipe and executes a command
io.read - Reads from the default input file
io.stderr - File handle for standard error file
io.stdin - File handle for standard input file
io.stdout - File handle for standard output file
io.tmpfile - Returns a handle to a temporary file
io.type - Returns type of file handle
io.write - Writes to the default output file
Topics
Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting
Scripting callbacks - plugins
(Help topic: lua=f:read)