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: 462
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.
User avatar
dusoft
Party member
Posts: 781
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post by dusoft »

RNavega wrote: Thu Jan 09, 2025 12:10 am
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.
Thanks, RNavega. Users can edit wiki indeed and they also should, if they encounter strange behavior or broken code examples. This way they help others in the community.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 15 guests