在Lua中获取外部txt文件中的最新一行

我有一个 lua 函数用于读写 txt 文件,每次 lua 写入时我需要在新行上写入,而不是替换之前的写入。我该如何做?我需要每次都读取并获取行吗?

以下是我的代码:

local function FileOutput(name)
    local f = io.open(name, "w+")
    local meta = {
        __call = function(t, str) f:write(str .. '\n') end,
        __gc = function() f:close() end
    }
    return setmetatable({}, meta)
end

function writeRec()
LOG("writing")
local testfile = FileOutput(getScriptDirectory()..'/textOutput.txt')
testfile('oh yes!')
testfile = nil
end

原文链接 https://stackoverflow.com/questions/7631152

点赞
stackoverflow用户148870
stackoverflow用户148870

你尝试过使用 a+ 而不是 w+ 吗?

http://www.lua.org/manual/5.1/manual.html#pdf-io.open

2011-10-03 05:04:42