io

This module provies access to high i/o primitives and streams.

File object

class io.File()

Class representing a stdio stream.

This class created through io.open() or io.fdopen(), never directly.

io.File.path

Returns the opened file’s path.

io.File.fd

Returns the file descriptor associated with the file.

io.File.mode

Returns the mode in which the file was opened.

io.File.closed

Boolean flag indicating if the file was closed.

io.FIle.prototype.read(nread)

Read data from the file.

Arguments:
  • nread – Amount of data to receive. If not specified it defaults to 4096. Alternatively, a Buffer can be passed, and data will be read into it.
Returns:

The data that was read as a string or the amount of data read as a number, if a Buffer was passed.

See also

fread(3)

io.FIle.prototype.readLine(nread)

Similar to io.FIle.prototype.read(), but stops at the newline (\n) character. This is the recommended function to read from stdin, but not from binary files.

See also

fgets(3)

io.FIle.prototype.write(data)

Write data on the file.

Arguments:
  • data – The data that will be written (can be a string or a Buffer).
Returns:

The number of bytes from data which were actually written.

See also

fwrite(3)

io.FIle.prototype.writeLine(data)

Same as io.FIle.prototype.write(), but add a newline (\n) at the end.

io.FIle.prototype.flush()

Flush the buffered write data to the file.

See also

fflush(3)

io.FIle.prototype.close()

Close the file.

Functions

io.open(path, mode[, buffering])

Opens the file at the given path in the given mode. Check fopen(3) for the mode details. It returns a io.File() object.

If buffering is specified, it must be -1 (for default buffering), 0 (for unbuffeered) or 1 for line buffering). See setvbuf(3).

io.fdopen(fd, mode[, path][, buffering])

Opens the fiven file descriptor in fd as a io.File() object. The given mode must be compatible with how the file descriptor was opened. path is purely informational.

If buffering is specified, it must be -1 (for default buffering), 0 (for unbuffeered) or 1 for line buffering). See setvbuf(3).

See also

fdopen(3)

io/select

This submodule provides access to select(2).

select.select(rfds, wfds, xfds, timeout)

Wait until any of the given file descriptors are ready for reading, writing or have a pending exceptional condition.

Arguments:
  • rfds – Array of file descriptors to monitor for reading.
  • wfds – Array of file descriptors to monitor for writing.
  • xfds – Array of file descriptors to monitor for pending exceptional conditions.
  • timeout – Amount of time to wait. null means unlimited. This function might return early if interrupted by a signal.
Returns:

An object containing 3 properties: rfds, wfds and xfds, containing the file descriptors which are ready for each condition respectively.

For more information see select(2).

io/poll

This submodule provides access to poll(2).

poll.POLLIN
poll.POLLOUT
poll.POLLPRI
poll.POLLRDHUP
poll.POLLERR
poll.POLLHUP
poll.POLLINVAL

Constants to be used in the events or revents fields of a pollfd object. Check poll(2) for more information. Note that not all these constants might be available on your platform.

poll.poll(pfds, timeout)

Examines the given file descriptors to see if some of them are ready for i/o or if certain events have occurred on them.

Arguments:
  • pfds – An array of pollfd objects to be examined. A pollfd object is any object which has a fd and a events properties. The events property must contain the or-ed events that the user is interested in examining.
  • timeout – Amount of time to wait. null means unlimited. This function might return early if interrupted by a signal.
Returns:

An array of pollfd objects, containing fd, events and revents properties. fd and events match the given ones, and revents indicates the received events.

For more information see poll(2).