【问题标题】:Lua: getting the latest line in external txt fileLua:获取外部txt文件中的最新行
【发布时间】:2011-10-03 04:56:35
【问题描述】:

我有一个 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

【问题讨论】:

  • 我不太确定__gc 是否适用于表,正如参考人员所说:“在其元表中带有字段 __gc 的垃圾 userdata 不会被立即收集垃圾收集器”

标签: lua


【解决方案1】:

您是否尝试过a+ 而不是w+

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

【讨论】:

  • 哦,是的!我真傻,我忘了检查写作模式。谢谢你,伙计!
猜你喜欢
  • 2020-12-13
  • 2014-06-05
  • 2015-08-26
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 2019-05-14
相关资源
最近更新 更多