| Lua io functions |
|---|
| Lua io functions These are the functions in the "io" table. Implicit operations (eg. io.read, io.write) use the standard file descriptors io.stdin, io.stdout, and io.stderr. You can operate on other files by getting a "file descriptor" from io.open, io.input, or io.output, and using that. Note the syntactical difference, that you follow a file descriptor by a colon, eg. f:close () Also see the description for the "os" table for operating system functions that do things like remove and rename files. io.close (f) Equivalent to f:close (). Without f supplied closes the default output file. io.flush () Equivalent to f:flush () for the default output file. Flushes outstanding data to disk. f = io.input (filename) Opens filename for input in text mode. Raises an error if it cannot. If opened OK returns a handle to the opened file, and makes it the default input file. If called with no argument, returns the handle to the default input file. If called with a file handle, sets the default input file to that handle. io.lines (filename) Opens filename for input in text mode. Raises an error if it cannot. If opened OK returns an iterator function that reads the file line-by-line. At end of file, returns nil and closes the file. If filename not supplied, uses the default input file. f = io.open (filename, mode) Opens a file and returns a file handle for working with it. Modes can be a string which is:
If the file cannot be opened this function does not raise an error (unlike io.input and io.output) but returns 3 things:
f = io.output (filename) Same behaviour as io.input, except it operates over the default output file. io.popen (command, mode) Creates a pipe and executes a command. Mode can be one of:
However io.popen is not supported under the version compiled into MUSHclient, so don't get too excited. :) This is an example of using popen under the Linux Lua executable: An alternative to using pipes, if you want to capture operating system output, is to redirect command output to a temporary file, like this: io.read () Equivalent to io.input ():read. See below for a description. f = io.tmpfile () Returns a handle to a temporary file, opened in update mode. Automatically removed when the program ends. io.stdin A file descriptor representing the current stdin (input) file handle. io.stdout A file descriptor representing the current stdout (output) file handle. io.stderr A file descriptor representing the current stderr (error) file handle. io.type () Returns a string which is one of:
io.write () Equivalent to io.output ():write. See below for a description. The operations below depend on having an open file handle returned from io.open, io.input, or io.output. f:close () Closes the file f. f:flush () Flushes outstanding data to disk. f:lines (filename) Returns an iterator function that reads the file line-by-line. At end of file, returns nil. It does not close the file. f:read (format1, format2, ...) Reads the file f according to the given formats. Each format returns a string, a number, or nil if it fails. The formats are:
f:seek (whence, offset) Sets and gets the current file position. f:seek () --> returns the current file position When setting a position you can supply a string which is one of:
The offset is a number from the base position. file:setvbuf (mode, size) Sets the buffering mode for an output file. There are three available modes, which are supplied as strings:
For the last two cases, size specifies the size of the buffer, in bytes. The default is an appropriate size. f:write (v1, v2, v3, ...) Writes arguments to the file. They must be strings or numbers. See Also ... Topics
Lua base functions
Lua coroutine functions
Lua debug functions
Lua math functions
Lua os functions
Lua package functions
Lua script extensions
Lua string functions
Lua table functions(Help topic: general=lua_io)
Documentation contents page |