Page 1 of 1

love.filesystem.lines is closing the file when it reaches the eof?

Posted: Tue Jan 07, 2025 1:38 am
by hyreia
I didn't see it in the documentation and I am wondering if this is expected behavior.

Code: Select all

function love.load()
  local file = love.filesystem.newFile("example")
  local ok, err = file:open("r")
  if not ok then assert(ok, err) end

  print("Is file open: "..tostring(file:isOpen()))
  for line in file:lines() do
    print(line)
    print("File is still open: "..tostring(file:isOpen()))
  end
  print("Is file open after reading in all the lines: "..tostring(file:isOpen()))
  
  print("When I try to close the file it returns: "..tostring(file:close()))
  
end
Returns this:
Is file open: true
First line.
File is still open: true
Second line.
File is still open: true
Third line.
File is still open: true
Here's a fourth.
File is still open: true
A fifth.
File is still open: true
A minor fall.
File is still open: true
A major lift.
File is still open: true
The broken code composing
File is still open: true
hallelujah
File is still open: true
Is file open after reading in all the lines: false
When I try to close the file it returns: false
I stumbled across this when trying to use this https://love2d.org/wiki/SICK and was wondering why I kept getting unexpected behavior.

So, I can work around this, but I'm wondering if there's any other gotchas like this. Like does read() work this way?
Also I noticed file:open() doesn't like full filepaths like using love.filesystem.getWorkingDirectory()...'/'..filename, it always says the file doesn't exist.

Re: love.filesystem.lines is closing the file when it reaches the eof?

Posted: Tue Jan 07, 2025 2:08 pm
by kov_serg
Do it by hand

Code: Select all

local function lines(f) return function() return f:read"l" end end

local f,err=io.open("dummy.lua","rb") if err then error(err) end
for line in lines(f) do print(line) end
f:close()

Re: love.filesystem.lines is closing the file when it reaches the eof?

Posted: Wed Jan 08, 2025 8:13 pm
by hyreia
Potentially related, I'm starting to wonder if this is a Windows 11 issuee. I recently had to upgrade and now it sometimes calls a resize event (love.resize) if I click out of the game window and click back in, yet it passes NO values into the resize event (w and h are nil). So it'll crash if I don't check them.

Re: love.filesystem.lines is closing the file when it reaches the eof?

Posted: Thu Jan 09, 2025 12:10 am
by RNavega
hyreia wrote: Tue Jan 07, 2025 1:38 am I didn't see it in the documentation and I am wondering if this is expected behavior.
Checking the source, yes, the file is explicitly closed if :lines() reaches EOF: Does sound like a useful thing to have in the wiki.
It turns out that forum users can edit the wiki, so I went and added a Note section to the File:lines() entry.