lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2011/4/19 Gilles Ganault <gilles.ganault@free.fr>:
> On Tue, 19 Apr 2011 04:26:46 -0500, Jeff Pohlmeyer
> <yetanothergeek@gmail.com> wrote:
>>Note that you need to pass a file opened with io.open("your filename")
>>to the function. My guess is you are trying to give it a string.
>
> Thanks for the tip. The page doesn't mention this. After making the
> change, I'm getting the error "attempt to index local 'file' (a nil
> value)" on line 2:
>
> =========== funcs.lua
> function fsize (file)
>        local current = file:seek()      -- get current position
>        local size = file:seek("end")    -- get file size
>        file:seek("set", current)        -- restore position
>        return size
> end
> =========== checklibs.lua
> dofile("funcs.lua")
>
> libs_file = io.open("librt.so.0","r")
> print(fsize(libs_file))
> libs_file:close()
> ===========
>
> Am I missing some module it needs for file operation?

You have to check the return value from io.open. It may be nil, and
followed by an error message:

libs_file,msg = io.open("librt.so.0","r")
if not libs_file then
  error(msg)
end
print(fsize(libs_file))
libs_file:close()