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
.