加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何从Lua文件中读取数据

发布时间:2020-12-15 00:23:30 所属栏目:大数据 来源:网络整理
导读:我想知道有没有办法从文件读取数据,或者只是看看它是否存在并返回true或false function fileRead(Path,LineNumber) --..Code... return Dataend 解决方法 尝试这个: -- http://lua-users.org/wiki/FileInputOutput-- see if the file existsfunction file_
我想知道有没有办法从文件读取数据,或者只是看看它是否存在并返回true或false
function fileRead(Path,LineNumber)
  --..Code...
  return Data
end

解决方法

尝试这个:
-- http://lua-users.org/wiki/FileInputOutput

-- see if the file exists
function file_exists(file)
  local f = io.open(file,"rb")
  if f then f:close() end
  return f ~= nil
end

-- get all lines from a file,returns an empty 
-- list/table if the file does not exist
function lines_from(file)
  if not file_exists(file) then return {} end
  lines = {}
  for line in io.lines(file) do 
    lines[#lines + 1] = line
  end
  return lines
end

-- tests the functions above
local file = 'test.lua'
local lines = lines_from(file)

-- print all line numbers and their contents
for k,v in pairs(lines) do
  print('line[' .. k .. ']',v)
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读