gpt4 book ai didi

lua - Lua如何从文件中读取数据

转载 作者:行者123 更新时间:2023-12-03 09:01:48 25 4
gpt4 key购买 nike

我想知道是否有办法从文件中读取数据,或者只是看看它是否存在并返回 truefalse

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

关于lua - Lua如何从文件中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11201262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com