A writer that writes data to a file or file descriptor.
luarocks install io-writer
the following functions return the error
object created by https://github.com/mah0x211/lua-errno module.
create a new writer instance that writes data to a file or file descriptor.
NOTE
this function uses the dup
system call internally to duplicate a file descriptor. thus, data can be write to a file even if the passed file is closed.
Parameters
f:file*|string|integer
: file, filename or file descriptor.sec:number
: timeout seconds. ifnil
or<0
, wait forever.
Returns
w:writer
: a writer instance.err:any
: error message.
Example
local dump = require('dump')
local writer = require('io.writer')
local f = assert(io.tmpfile())
local w = writer.new(f)
local n, err, again, remain = w:write('hello', ' writer ', 'world!')
f:seek('set')
print(dump({
n = n,
err = err,
again = again,
remain = remain,
content = f:read('*a'),
}))
-- {
-- content = "hello writer world!",
-- n = 19
-- }
get the file descriptor of the writer.
Returns
fd:integer
: file descriptor.
set the timeout seconds.
Parameters
sec:number
: timeout seconds. ifnil
or<0
, wait forever.
close the writer.
Returns
ok:boolean
:true
if succeeded.err:any
: error message.
write data to the file or file descriptor.
NOTE
if the file descriptor's peer is closed, this method returns nothing.
Parameters
data:any
: data to write. if a non-string value is specified, it is converted to a string bytostring
function....:any
: additional data to write. these are concatenated withdata
.
Returns
n:integer
: number of bytes written.err:any
: error message.timeout:boolean
:true
if timed out.