Reputation: 11
I am logging video files I have played through MPV and while using io I want it to create one line per video. Using the inital options it merley deletes it, and upon going to next video, it won't add new line by default.
I think the script should be close to working. However, this issue stands in my way: "bad argument #1 to '(for generator)' (invalid option). Apparently, something is wrong with the for loop, but I am unable to pinpoint it and would appreciate a pair of hands in solving this problem as I am still learning lua.
Here's the code so far:
if not paused then totaltime = totaltime + os.clock() - lasttime end
message = (totaltime .. "s, " .. timeloaded .. ", " .. filename)
local file = io.open(logpath, "r+")
local lines = {}
if file_exists(logpath) then
for l in file:lines('L') do
if not l:find(message, 1, true) then
lines[#lines+1] = 1
file.write(message)
file:close()
end
end
end
end
Upvotes: 0
Views: 322
Reputation: 28974
The problem is this line
for l in file:lines('L') do
You're probably running Lua 5.1 which does not support that option 'L' for file:lines
Just use
for l in file:lines() do
Also if you close the file in the generic for loop you'll cause an error for attempting to read from an already closed file. You should break the loop after closing the file.
It says bad argument to write (FILE* expected, got string).
Replace file.write(message)
with file:write(message)
which is short for file.write(file, message)
. This function actually needs two arguments. One is the file itself which is implicitly provided when using the colon syntax.
If you just want to add a line to an existing file you don't need to read and check all the lines. Just open the file with option 'a' to open it in append mode.
local file = io.open(logpath, "a")
if file then
file:write("\nThis is a new line")
file:close()
end
Upvotes: 0