6.8 – Input and Output Facilities

The I/O library provides two different styles for file manipulation. The first one uses implicit file handles; that is, there are operations to set a default input file and a default output file, and all input/output operations are over these default files. The second style uses explicit file handles.

When using implicit file handles, all operations are supplied by table io. When using explicit file handles, the operation io.open returns a file handle and then all operations are supplied as methods of the file handle.

The table io also provides three predefined file handles with their usual meanings from C: io.stdin, io.stdout, and io.stderr. The I/O library never closes these files.

Unless otherwise stated, all I/O functions return nil on failure (plus an error message as a second result and a system-dependent error code as a third result) and some value different from nil on success. On non-POSIX systems, the computation of the error message and error code in case of errors may be not thread safe, because they rely on the global C variable errno.

file:read()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

file:read (···) Reads the file file, according to the given formats, which specify what to read. For each format, the function returns a string or a number with the

2025-01-10 15:47:30
file:flush()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

file:flush () Saves any written data to file.

2025-01-10 15:47:30
file:write()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

file:write (···) Writes the value of each of its arguments to file. The arguments must be strings or numbers. In case of success, this function returns file

2025-01-10 15:47:30
file:close()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

file:close () Closes file. Note that files are automatically closed when their handles are garbage collected, but that takes an unpredictable amount of time to happen

2025-01-10 15:47:30
io.tmpfile()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

io.tmpfile () In case of success, returns a handle for a temporary file. This file is opened in update mode and it is automatically removed when the program ends.

2025-01-10 15:47:30
file:setvbuf()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

file:setvbuf (mode [, size]) Sets the buffering mode for an output file. There are three available modes: "no": no buffering; the result of

2025-01-10 15:47:30
io.open()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

io.open (filename [, mode]) This function opens a file, in the mode specified in the string mode. In case of success, it returns a new file handle. The mode

2025-01-10 15:47:30
io.lines()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

io.lines ([filename, ···]) Opens the given file name in read mode and returns an iterator function that works like file:lines(···) over the opened file. When the

2025-01-10 15:47:30