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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
hyreia
Prole
Posts: 2
Joined: Tue Jan 07, 2025 1:29 am

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

Post 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.
kov_serg
Prole
Posts: 12
Joined: Wed Dec 18, 2024 4:38 pm

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

Post 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()
hyreia
Prole
Posts: 2
Joined: Tue Jan 07, 2025 1:29 am

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

Post 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.
RNavega
Party member
Posts: 401
Joined: Sun Aug 16, 2020 1:28 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 7 guests