Lua:读取文件

我正在使用Lua读取文件中的数据,代码如下:

   filename = "./temp/vtx_vel"..id..".dat"
   file = io.open(filename, "r")
   lineno = i + ni*j
   local n = -1
   for l in io.lines(filename) do
       n = n + 1
       if n == lineno then
          vel = tonumber(l)
          break
       end
   end
   file:close()

外部文件中的数据正在变化。然而,令我感到奇怪的是,在不同步骤中读取此文件时,我得到的是相同的值。为什么会这样呢?

谢谢。

点赞
用户4333718
用户4333718

根据我的经验,从文件中读取内容最好的方式就是这样做:

local f = io.open(--在这里输入你的文件名--)
local output = {}
for each in f:lines() do
  output[#output+1] = each
end

你的文件内容将会被读取到表 output 中。 还有一点需要注意的是,如果没有定义模式,io.open() 函数默认以 "r" 或读取模式打开文件。

2015-02-14 07:03:12