lua-users home
lua-l archive

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


Hi!

2011/3/19 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> Yes, the file handle will still be open, at least until the file
>> object is garbage collected. There is no way for an iterator to detect
>> a broken loop and run cleanup code, so you will need to make sure the
>> handle is closed explicitly.
>
> Or simply leave it to the garbage collection. (More often than not there
> is no hurry to close the file.)

Hmm, until now it bit me twice not doing so (and doing business in C++
I tend not to close files opened in function scope, leaving the work
to the destructor).

Look at the following simple example:

local function f( ... )
    local of = io.open( 'x.txt', 'w' )
    of:write( ... )
end

for i = 1, 100 do
    f( i, '\n' )
end

This does not yield a file containing '100' on my system...

Regards,
Matthias