lua-users home
lua-l archive

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


Alexander Gladysh wrote:

> local f = assert(io.open("123.txt"))
> for l in f:lines() do

Firs of all, I am wondering why it works ion the first place. According
to the manual, you need io.lines(filename), not io.lines(filehandle)!
If it works with filehandle, it is not documented, so the behaviour in
unexpected.

> Looks like subsequent calls for :lines() on the same handle are ignored.

As it is with other iterators, which - when finished - return nil. This
is expected.

> So, the correct way is to re-open nested file handles on each:

The io.lines() manual says: "When the iterator function detects the end
of file, it returns nil (to finish the loop) and automatically closes
the file.".

So I am expecting the behaviour you observe. You seem to expect that
after the end of file, iterator closes and reopens the file, rewinding
to the beginning. But it is not true, if you want to read the file many
times in your internal loop, you need to open it many times, as you have
just noticed.

Regards,
miko