Reads data from a specified file descriptor.
luarocks install io-read
the following functions return the error object created by https://github.com/mah0x211/lua-errno module.
Reads data from the specified file handle or file descriptor.
Parameters
file:file*|integer: a file handle or a file descriptor.count:integer: the number of bytes to read. if thecountis not specified, then it will be read until the end of the file. (default:nil)offset:integer: the offset in bytes from the beginning of the file to start reading. if theoffsetis specified, then the file position will be moved to the specified offset before reading. (default:nil)
Returns
data:string: read data.err:any: error object.again:boolean:trueif the read operation is incomplete (read syscall returnedEAGAIN,EWOULDBLOCK, orEINTR) and thedatais not available.
local read = require('io.read')
local f = assert(io.open('./test.txt'))
-- read 10 bytes from the file
local data, err, again = read(f, 10)
if data then
print(data)
elseif again then
print('read syscall returned EAGAIN or EWOULDBLOCK')
elseif err then
print('read syscall is failed:', err)
else
print('end of file')
end