为什么即使文件中只有 0,它仍在执行?

Why is it still executing even if the file only has a 0 in it?

这应该检查文件中是否有零,如果不是零,它应该执行代码。 但是它仍然执行代码,即使它只有一个零。

f = io.open("timesave.txt", "r")
if (f ~= 0) then
    io.input(f)
    resultstart = f:read("*line")
    resultstop = f:read("*line")
    f:close()
end

io.open("timesave.txt", "r") returns 如果成功或为零,则为文件句柄。

发件人:https://www.lua.org/manual/5.4/manual.html#pdf-io.open

io.open (filename [, mode])

This function opens a file, in the mode specified in the string mode. In case of success, it returns a new file handle. ...

您不能使用 io.open 检查文件是否包含零。

为此,您需要打开文件,阅读并检查其内容。

-- open the file in read mode
local f = io.open("timesave.txt", "rb")
-- check wether the file has been opened
if not f then print("failed to open file") return end
-- read and check wether there is only a 0 in the file.
if f:read("a") == "[=10=]" then print("file only contains \0") end
f:close()