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.

io.type()

io.type (obj) Checks whether obj is a valid file handle. Returns the string "file" if obj is an open file handle, "closed file"

2017-02-21 04:11:34
io.flush()

io.flush () Equivalent to io.output():flush().

2017-02-21 04:11:28
io.popen()

io.popen (prog [, mode]) This function is system dependent and is not available on all platforms. Starts program prog in a separated process and returns

2017-02-21 04:11:32
file:seek()

file:seek ([whence [, offset]]) Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by

2017-02-21 04:11:03
io.read()

io.read (···) Equivalent to io.input():read(···).

2017-02-21 04:11:32
io.output()

io.output ([file]) Similar to io.input, but operates

2017-02-21 04:11:31
file:lines()

file:lines (···) Returns an iterator function that, each time it is called, reads the file according to the given formats. When no format is given, uses "l" as a

2017-02-21 04:11:01
io.input()

io.input ([file]) When called with a file name, it opens the named file (in text mode), and sets its handle as the default input file. When called with a file handle, it simply

2017-02-21 04:11:29
io.write()

io.write (···) Equivalent to io.output():write(···).

2017-02-21 04:11:35
file:read()

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

2017-02-21 04:11:02