Lua:读一个文件

时间:2015-02-12 03:30:22

标签: 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() 

外部文件中的数据正在发生变化。但是,对我来说很奇怪,我在不同的步骤读取这个文件时得到了相同的值。那是为什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

根据我的经验阅读文件的最佳方法是执行此操作:

local f = io.open(--your file here)
local output = {}
for each in f:lines() do
  output[#output+1] = each
end

您的文件将被读入表格output。 除非您定义模式,否则需要注意的其他事项,io.open()默认情况下以“r”或读取模式打开文件。

相关问题