我正在尝试让我的 Lua 程序从我存储在计算机上的文件中读取。我正在使用这段代码:
function readFile(file)
local file = io.open(file,"rb")
local content = file:read("*all")
file:close()
return content
当我尝试运行它时,我收到此错误:
lua: io.lua:3: attempt to index local 'file' (a nil value)
stack traceback:
io.lua:3: in main chunk
[C]: ?
exit code: 1
我找到了原始问题的解决方案,但是当我运行脚本时,它报告变量“常量”为零。
输入:
function readFile(filename)
local file = io.open(filename,"rb")
local content = file:read("*a")
file:close()
return content
end
readFile("test.txt")
print(content)
输出:
lua -e "io.stdout:setvbuf 'no'" "io.lua"
nil
Exit code: 0